当前位置: 编程技术>移动开发
本页文章导读:
▪QQ作派的列表 QQ风格的列表
头文件:
#import <UIKit/UIKit.h>
@interface QQstyleViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
UITableView *table;
NSMutableArray *array;
BOOL *flag;
}
@prop.........
▪ 怎么设置每个ListView item的背景色,但是保持选中时selector的默认颜色 怎样设置每个ListView item的背景色,但是保持选中时selector的默认颜色?
要设置ListView item的背景色很简单, 只要调用contentView.setBackground就行了。 但是设置之后会发现,按住item后没有显示.........
▪ 让TextView尺码跑马灯效果 让TextView尺寸跑马灯效果
首先,使用TextView实现走马灯形式的滚动显示,只需要对其设置两个属性:android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"但是,TextView的滚动显示,有一个前.........
[1]QQ作派的列表
来源: 互联网 发布时间: 2014-02-18
QQ风格的列表
头文件:
#import <UIKit/UIKit.h>
@interface QQstyleViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
UITableView *table;
NSMutableArray *array;
BOOL *flag;
}
@property (nonatomic, retain) UITableView *table;
@end
实现文件:
#import "QQstyleViewController.h"
@implementation QQstyleViewController
@synthesize table;
- (void)viewDidLoad {
[super viewDidLoad];
table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
NSArray *ary1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", nil];
NSArray *ary2 = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
NSArray *ary3 = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];
array = [[NSMutableArray alloc] initWithObjects:ary1, ary2, ary3, nil];
[ary1 release];
[ary2 release];
[ary3 release];
flag = (BOOL *) malloc([array count] * sizeof(BOOL *));
memset(flag, NO, sizeof(flag));
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [array count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (flag[section]) {
return [(NSArray *)[array objectAtIndex:section] count];
}
else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *str = [[array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = str;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = section;
[btn addTarget:self action:@selector(headerClicked:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
-(void)headerClicked:(id)sender
{
int sectionIndex = ((UIButton *)sender).tag;
flag[sectionIndex] = !flag[sectionIndex];
[table reloadData];
}
- (void)dealloc {
free(flag);
[array release];
[table release];
[super dealloc];
}
@end
示例图:
[2] 怎么设置每个ListView item的背景色,但是保持选中时selector的默认颜色
来源: 互联网 发布时间: 2014-02-18
怎样设置每个ListView item的背景色,但是保持选中时selector的默认颜色?
要设置ListView item的背景色很简单, 只要调用contentView.setBackground就行了。 但是设置之后会发现,按住item后没有显示默认的选中的颜色,这个也应该很很好理解,因为你把backgroud设成了单一的颜色。 其实我们只要设置为一个draweable selector就行了。 把选中时的颜色设为完全透明。
ref:http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android
要设置ListView item的背景色很简单, 只要调用contentView.setBackground就行了。 但是设置之后会发现,按住item后没有显示默认的选中的颜色,这个也应该很很好理解,因为你把backgroud设成了单一的颜色。 其实我们只要设置为一个draweable selector就行了。 把选中时的颜色设为完全透明。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<solid android:color="@color/gray"/>
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="#00000000"/>
</shape>
</item>
</selector>ref:http://stackoverflow.com/questions/2217753/changing-background-color-of-listview-items-on-android
[3] 让TextView尺码跑马灯效果
来源: 互联网 发布时间: 2014-02-18
让TextView尺寸跑马灯效果
首先,使用TextView实现走马灯形式的滚动显示,只需要对其设置两个属性:
android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
但是,TextView的滚动显示,有一个前提,TextView需要必须处于focus状态。当TextView失去焦点的时候,TextView将会停止滚动。如何实现无限滚动,当然也需要从焦点入手。当然,直接requestFocus()是不行的,这里我使用了另外一个方法。观察到textView有一个名为isFocused()的方法,文档中的注释是这样的:
也就是说当TextView拥有焦点的时候会返回true.同时可以发现,TextView中很多地方都是直接调用这个方法作为判断条件,最关键的,这个方法被声明为public! ok, 实现方法已经初现端倪了!
做法是这样:自定义一个OOXXTextView, 继承自TextView, 同时override isFocused()方法,并使其返回值为true, 样例如下:
public class AlwaysMarqueeTextView extends TextView{
public AlwaysMarqueeTextView(Context context) {
super(context);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
}
运行代码~一切OK!
最新技术文章: