
btn.frame = CGRectMake(x, y, width, height)
[btn setTitle: @"search" forState: UIControlStateNormal]
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]] //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0]
[btn seBackgroundColor: [UIColor blueColor]]
//最后将按钮加入到指定视图superView
[superView addSubview: btn]
附:创建按钮的两种方法:
1、动态创建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect]
[btnfont setFrame:CGRectMake(100, 10, 120, 40)]
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]
[btnfont setTitle:@"字体" forState:UIControlStateNormal]
btnfont.backgroundColor=[UIColor clearColor]
[self.view addSubview:btnfont]
2、在xib文件中已经创建好,通过tag获取按钮
UIButton *testButton= (UIButton*)[self.view viewWithTag:100]
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside]
注册事件
-(void) test: (id) sender{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease]
[av show]
}
2.设置UIButton的文字显示位置、字体的大小、字体的颜色
btn.frame = CGRectMake(x, y, width, height)
[btn setTitle: @"search" forState: UIControlStateNormal]
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]] //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0]
[btn seBackgroundColor: [UIColor blueColor]]
//最后将按钮加入到指定视图superView
[superView addSubview: btn]
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)]
这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,
btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft //设置文字位置,现设为居左,默认的是居中
[btn setTitle:@“title”forState:UIControlStateNormal]// 添加文字
有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0)
使文字距离做边框保持10个像素的距离。
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]]
btn.titleLabel.textColor=[UIColor redColor]
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal]
1、把手机屏幕分成上下。上下两部分都采用Linearlayout方式布局<LinearLayout><LinearLayout> 上半部分</LinearyLayout><LinearLayout> 下半部分</LinearyLayout></LinearLayout>2、下半部分LinearLayout高度固定,上半部分LinearyLayout设置layout_weight权重,占满剩余屏幕空间<LinearLayout><LinearLayout android:layout_height="wrap_content" android:llayout_weight="1"> //设置高度自适应,并且权重为1</LinearyLayout><LinearLayout android:layout_height="50px"> //下半部分设置高度固定</LinearyLayout></LinearLayout>3、下半部分LinearLayout中添加按钮,设置android:gravity右对齐。<LinearLayout android:layout_height="50px" android:gravity="right"> //下半部分设置高度固定 <button andtoid:text="右下角按钮"/></LinearyLayout>欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)