当前位置: 编程技术>移动开发
本页文章导读:
▪UITableViewCell的应用——自定义tableView视图 UITableViewCell的使用——自定义tableView视图
视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不.........
▪ 自定义式样(style)与主题(theme) 自定义样式(style)与主题(theme)
Android提供了许多可视的组件。通过自定义样式和主题,可以避免用这些组件开发的应用看上去千篇一律。样式和主题都是通过预定义一系列属性值来形成统一的.........
▪ 禁用button的步骤 禁用button的方法
在AppWidget开发中我们可以使用button,但是却不能是buttondisable为什么呢 RemoteViews不能控制一个button可用和不可用的状态,但是可以控制它的显示与隐藏 因为我们可以利用这个.........
[1]UITableViewCell的应用——自定义tableView视图
来源: 互联网 发布时间: 2014-02-18
UITableViewCell的使用——自定义tableView视图
视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下
首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码
完成.m中代码
若不需要cell则改为:
static NSString *CellIdentifier2 = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.textLabel.text =@"CCCCCCCC";
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
// cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sign_10x12.png"]];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[cell.textLabel setTextColor:[UIColor colorWithRed:0/255.0 green:106/255.0 blue:166/255.0 alpha:1.0]];
return cell;
下面我们新建一个类,注意subclass选择UITableViewCell,名称为MyCell,生成之后再创建相应的xib文件
双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标
在.h文件中完成代码
在.m中完成代码
最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。(注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了)
视图需要我们自己去定义样式时用到了TableViewCell组件,效果如下
首先创建View Based App工程,在.xib文件中拖入一个Table View,前面我们说到了,这里就不再重复,注意连接协议和两个必须方法的实现。完成.h中代码
#import <UIKit/UIKit.h>
@interface TableViewCellViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *tView;
}
@property (nonatomic,retain)UITableView *tView;
@end完成.m中代码
#import "TableViewCellViewController.h"
#import "MyCell.h"
@implementation TableViewCellViewController
@synthesize tView;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[tView release];
[super dealloc];
}
-(NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 9;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
[[cell lable] setText:@"31"];
[[cell lable1] setText:@"Raul"];
[[cell myImage] setImage:[UIImage imageNamed:@"3316.jpg"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120;
}
@end若不需要cell则改为:
static NSString *CellIdentifier2 = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.textLabel.text =@"CCCCCCCC";
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
// cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sign_10x12.png"]];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[cell.textLabel setTextColor:[UIColor colorWithRed:0/255.0 green:106/255.0 blue:166/255.0 alpha:1.0]];
return cell;
下面我们新建一个类,注意subclass选择UITableViewCell,名称为MyCell,生成之后再创建相应的xib文件
双击MyCell.xib,将Table View Cell拖入主窗口中,并且删除原主窗口中的View图标
在.h文件中完成代码
#import <UIKit/UIKit.h>
@interface MyCell : UITableViewCell {
IBOutlet UILabel *lable;
IBOutlet UILabel *lable1;
IBOutlet UIImageView *myImage;
}
@property(nonatomic,retain) UILabel *lable;
@property(nonatomic,retain) UILabel *lable1;
@property (nonatomic,retain) UIImageView *myImage;
@end在.m中完成代码
#import "MyCell.h"
@implementation MyCell
@synthesize lable,lable1,myImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[myImage release];
[lable release];
[super dealloc];
}
@end
最后我们看一下MyCell.xib中的连接,按住Ctrl拖入将要显示区域的文字和图片找到相应的接口即可。(注意:是myCell和文字和图片连接,不是file’s owner和文字和图片连接,我又犯这个错误了)
[2] 自定义式样(style)与主题(theme)
来源: 互联网 发布时间: 2014-02-18
自定义样式(style)与主题(theme)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 定义my_style_1,没有指定parent,用系统缺省的 -->
<style name="my_style_1">
<!-- 定义与指定View相关的若干属性 -->
<item name="android:hint">load from style 1</item>
</style>
<!-- 定义my_style_2,用自定义的my_style_1作为parent -->
<style name="my_style_2" parent="@style/my_style_1">
<!-- 定义与指定View相关的若干属性 -->
<item name="android:textSize">30sp</item>
<item name="android:textColor">#FFFF0000</item>
<item name="android:hint">load from style 2</item>
</style>
<!-- 定义my_style_3,用android的EditText作为parent -->
<style name="my_style_3" parent="@android:style/Widget.EditText">
<!-- 定义与指定View相关的若干属性 -->
<item name="android:hint">"load from style 3"</item>
<item name="android:textStyle">bold|italic</item>
<item name="android:typeface">monospace</item>>
<item name="android:background">@drawable/mybackground</item>
</style>
<!-- 定义MyTheme,用android的Theme作为parent -->
<style name="MyTheme" parent="@android:style/Theme">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#FF0000FF</item>
<item name="android:hint">"load from style 3"</item>
<item name="android:textStyle">bold|italic</item>
<item name="android:typeface">monospace</item>>
<item name="android:background">@drawable/gallery_selected_pressed</item>
<item name="myStyle">@style/my_style_3</item>
</style>
</resources>
复制代码
主题和样式的定义方法类似,都是在<style>下添加N个<item>。
<style>下有两个有用的属性:
name - 以后引用时会用到;
parent - 可选,一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource,也可以是自己定义的style。
<item>下定义需要改变的属性值。Android中能使用的属性可以在<sdk>/docs/reference/android/R.styleable.html中查到;也可以用自己定义的属性值;
2.使用主题
a)从AndroidManifest中指定,可以选择Application应用级:
<application android:theme="@style/MyTheme">
或是Activity级: <activity android:theme="@style/MyTheme">
b)从Java代码中指定:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
setContentView(R.layout.main);
}
复制代码
注意:setTheme必须在setContentView(),addContentView()或inflate()等实例化View的函数之前调用!
3.使用样式
a)从layout的xml文件中指定:
<EditText android:id="@+id/EditText03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
复制代码
b)从Java代码中指定:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.MyTheme);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout)findViewById(R.id.llMain);
EditText et = new EditText(this, null, R.attr.myStyle);
ll.addView(et);
}
复制代码
Android提供了许多可视的组件。通过自定义样式和主题,可以避免用这些组件开发的应用看上去千篇一律。
样式和主题都是通过预定义一系列属性值来形成统一的显示风格。区别是,样式只能应用于某种类型的View;而主题刚好相反,它不能应用于特定的View,而只能作用于一个或多个Activity,或是整个应用。
以下结合具体例子说明如何定义样式和主题:
1.定义样式和主题
在工程中res/values/下添加styles.xml
<style>下有两个有用的属性:
name - 以后引用时会用到;
parent - 可选,一些在自定义的style中没有指定的属性会继承parent style中的值。parent可以是android预定义的resource,也可以是自己定义的style。
<item>下定义需要改变的属性值。Android中能使用的属性可以在<sdk>/docs/reference/android/R.styleable.html中查到;也可以用自己定义的属性值;
2.使用主题
a)从AndroidManifest中指定,可以选择Application应用级:
<application android:theme="@style/MyTheme">
或是Activity级: <activity android:theme="@style/MyTheme">
b)从Java代码中指定:
3.使用样式
a)从layout的xml文件中指定:
转载:http://www.cnblogs.com/wangtianxj/archive/2010/02/21/1670668.html
[3] 禁用button的步骤
来源: 互联网 发布时间: 2014-02-18
禁用button的方法
在AppWidget开发中我们可以使用button,但是却不能是buttondisable为什么呢
RemoteViews不能控制一个button可用和不可用的状态,但是可以控制它的显示与隐藏
因为我们可以利用这个造假
然后呢
<ButtonAndroid:idButtonAndroid:id="@+id/startbutton" android:text="Start" android:visibility="visible"> </Button> <Buttonandroid:idButtonandroid:id="@+id/startbutton_disabled" android:text="Start" android:clickable="false" androidandroid:textColor="#999999" android:visibility="gone"> </Button> <Buttonandroid:idButtonandroid:id="@+id/stopbutton" android:text="Stop" android:visibility="gone"> </Button> <Buttonandroid:idButtonandroid:id="@+id/stopbutton_disabled" android:text="Stop" android:clickable="false" androidandroid:textColor="#999999" android:visibility="visible"> </Button>
当点击startbutton的时候
RemoteViews remoteView=newRemoteView(context.getPackageName(),R.layout.widget); remoteView.setViewVisibility(R.id.startbutton,View.GONE); remoteView.setViewVisibility(R.id.startbutton_disabled,View.VISIBLE); remoteView.setViewVisibility(R.id.stopbutton,View.VISIBLE); remoteView.setViewVisibility(R.id.stopbutton_disabled,View.GONE); AppWidgetManager.getInstance(context).updateAppWidget(AppWidgetId,remoteView);
当点击stopbutton的时候
RemoteViewsremoteView=newRemoteViews(context.getPackageName(),R.layout.widget); remoteView.setViewVisibility(R.id.startbutton,View.VISIBLE); remoteView.setViewVisibility(R.id.startbutton_disabled,View.GONE); remoteView.setViewVisibility(R.id.stopbutton,View.GONE); remoteView.setViewVisibility(R.id.stopbutton_disabled,View.VISIBLE); AppWidgetManager.getInstance(context).updateAppWidget(AppWidgetId,remoteView);
其实通过一个android:clickable="false",还有buuton的隐藏转换造成了视觉的欺骗
最新技术文章: