常用的基本控件
TextView EditText Button ImageView ImageButton
控件常用属性
textView是EditText和button的父类,所以textView的属性,EditText和button都能用。ImageButton 是ImageView的子类,只能显示图片不能显示文本
一.对文本的操作
可设置字体内容,大小,颜色,样式
##### 二.对布局的操作
可用layout_height或height设置高度,同理也可设置宽度
###### layout_width 与width的区别是
layout_width 是指父布局允许控件所占的宽度。width指控件的自身宽度。如果layout_width和width都设置为具体值是width将不起作用,因为父布局已经给你分配空间了控件的宽度只能是他了,而如果属性值设置为wrap_content(包裹控件)那么width就起作用了
参考自
layout_width常用的属性值:match_parent(填充父布局),wrap_content(包裹控件)。
layout_gravity与gravity的区别
layout_gravity:是相对与父元素来说的子元素在什么位置,在子元素中设置
gravity:子元素在父元素中的什么位置,在父元素中设置,也可以设置元素本身文本显示的地方
<Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="right" android:text="Button3" />
三.对button绑定点击事件
第一种方式通过匿名内部类
Button.setOnclickListener(new OnClickLisnener(){
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub//点击按钮之后要做的事情
}
});
第二种.通过实现OnClickListener接口来实现
button.setOnClickListener(this);
public void onClick(View v)
{
switch(v.getId())
{
case R.id.XXXX:
}
}
第三种.在xml文件里面绑定点击事件
<button
android:onClick="click">
public void click(){
//点击后要执行的操作,方法名得与绑定的onClick属性值一样
}
四.设置权重
设置权重可以使控件平分掉多余的空间
<Button
android:id="@+id/chu"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="/"
android:textSize="10dp"
android:layout_weight="1"/>
android:layout_weight="1"在父元素中所占的比重,数越大比重越大,如果设置他则控件宽度等于,原宽度加上加上他所占的剩余控件比的宽度
五.设置背景图片
一.可通过普通控件的background属性设置
<Button
android:background="@drawable/button_pressed"/>
但是会拉伸变形,无法按比例缩放
二.通过Imagebutton控件
<ImageButton
android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/button_pressed" android:scaleType="centerInside" />
<!--src指定图片来源。scaleType指定拉伸类型-->
六.文本框特殊属性
(1) 设置文本框默认提示信息:android:hint
(2) 当用户把焦点切换到输入框时,自动选中已输入内容:android: selectAllOnFocus=“true”
(3) 设置只接受某种输入类型:android:inputType,取值可以为 numberPassword,number,date,phone
七.图片文字一起显示
<TextView
android:drawableLeft="@drawable/ic_launcher">
<!--左侧添加一个小图-->
线性布局
Linelayout 线性布局可以实现嵌套操作,类似于父子布局那种
设置元素的布局方式,默认水平布局(horizontal)下面代码为垂直布局
<LinearLayout
android:orientation="vertical">
</LinearLayout>
selector 选择器与shape
selector选择器
用户对控件操作后,而触发的一些界面上的变化
常用属性
<item android:drawable="@drawable/solid_stroke_pressed" android:state_focused="true" />
<item android:drawable="@drawable/solid_stroke_pressed" android:state_selected="true" />
<item android:drawable="@drawable/solid_stroke_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/solid_stroke_normal" />
<!--获取焦点时 选中时 按压时 默认-->
默认的必须放在最后
参考自
shape
对控件形状及细节的详细设置
常用属性
<shape> android:shape=["rectangle" | "oval" | "line" | "ring"]其中rectagle矩形,oval椭圆,line水平直线,ring环形(一般用矩形)
<!--圆角 四个角-->
<corners
android:radius="3dp" //圆角度数
android:topLeftRadius="3dp" //左上圆角度数
android:topRightRadius="3dp" //右上圆角度数
android:bottomLeftRadius="3dp" //左下圆角度数
android:bottomRightRadius="3dp" //右上圆角度数
/>
<!--渐变-->
<gradient>百度
</gradient>
<!--描边 及边框-->
<stroke
android:color="color" // 描边的颜色
android:width="integer" // 描边的宽度
android:dashGap="integer" // 虚线间隔
andro,id:dashWidth="integer" // 虚线宽度
></stork>
<!--填充-->
<solid
android:color="color" // shape的填充色
></solid>
<!--边距 内边距-->
<padding>