1.系统主题
2.背景选择器
android图片选择器和颜色选择器(合称背景选择器)
图片选择器:drawable中新建选择器xml文件imageselector.xml
<?xml version=“1.0” encoding=“utf-8”?>
<selector xmlns:android=“http://schemas.android.com/apk/res/android” >
<item android:state_pressed=“true” android:drawable=“@drawable/aa”/>
<item android:drawable=“@drawable/bb”/>
</selector>
颜色选择器:color中新建颜色选择器xml文件:colorselector.xml
<?xml version=“1.0” encoding=“utf-8”?>
<selector xmlns:android=“http://schemas.android.com/apk/res/android” >
<item android:state_pressed=“true” android:color=“#00FF00”/>
<item android:color=“#FF0000”/>
</selector>
在布局文件activty_main.xml文件中设置一个Button:
<Button
android:id=“@+id/btn”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“背景选择器测试”
android:background=“@drawable/imageselector”
android:textColor=“@drawable/colorselector”/>
运行效果:
点击该按钮后效果:
颜色选择器直接用在设置颜色的地方没问题,但是如background可以放图片也可以放颜色,当直接用颜色选择器报错。解决方法:把颜色当图片用,颜色不要直接写值,写死了,在colors.xml定义后引用即可。
colors.xml:
<?xml version=“1.0” encoding=“utf-8”?>
<resources>
<string name=“app_name”>AppUIDemo</string>
<string name=“hello_world”>Hello world!</string>
</resources>
将颜色选择器定义成图片选择器:
<?xml version=“1.0” encoding=“utf-8”?>
<selector xmlns:android=“http://schemas.android.com/apk/res/android” >
<item android:state_pressed=“true” android:drawable=“@color/color1”/>
<item android:drawable=“@color/color2”/>
</selector>
引用:
android:background=“@drawable/colorselector2”
运行效果:
点击后:
3. android UI android界面
3.1 TextView:
android:autoLink=“all” 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)
android:ellipsize=“marquee” 设置当文字过长时,该控件该如何显示,可选值start,end,marquee:滚动,跑马灯效果,不能设autoLink,并且只有获得焦点时才会有滚动效果,所以需设置:
android:focusableInTouchMode=“true”android:focusable=“true”
android:focusable=“true” 可以获得焦点
android:focusableInTouchMode=“true” 触摸可以获得焦点
android:marqueeRepeatLimit=“marquee_forever” 设置重复滚动的次数
android:singleLine=“true” 设置单行显示
android:textColorLink=“#ff0099” 文字链接的颜色
android:tag=“yes” View中添加一个tag,相当于一个兜,想放什么就放什么,用的时候再取出来
说明:Android系统中TextView实现跑马灯效果,必须具备以下几个条件:
1)、android:ellipsize=“marquee”
2)、TextView必须单行显示,即内容必须超出TextView大小
3)、TextView要获得焦点才能滚动
3.2 EditText:
3.2.1 EditText属性
android:digits=“12345” 设置只接受某些数字
android:inputType=“number” 设置输入文本的类型
android:numeric=“integer” 设置输入文本的类型,跟输入法有关系,不推荐使用了
android:phoneNumber=“true” 设置输入文本的类型(数字)
android:drawableBottom=“@drawable/icon”文字下方放一个图片
android:cursorVisible=“false” 设置光标是否可见
android:hint=“demo”
android:textColorHint=“@color/mycolor” 提示文字颜色
android:imeOptions=“actionGo” Enter键图标设置
可选值有:
actionGo actionSearch
|
|
actionSend actionNext
|
|
actionDone
android:lines=“3” 设置文本的行数
android:maxLines=“3” 设置文本的最大显示行数
android:minLines=“3”设置文本的最小行数
android:lineSpacingExtra=“10dp” 设置行间距
android:lineSpacingMultiplier=“2”行间距的倍数
android:password=“true”
android:visibility=“gone” 是否可见
android:textColor=“#ffcc00” 设置文本颜色
android:textColorHighlight =“#ffcc00”被选中文字的底色 ,默认为蓝色
android:textColorHint =“#ffcc00” 设置提示信息文字的颜色,默认为灰色
android:editable=”false”
android:enabled=“false” 设置是否可用
android:textScaleX =“1” 文字放大比例
android:textSize =“30sp”
android:textStyle 设置字形[bold(粗体) 0,italic(斜体)]
android:typeface 设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3
android:height="10dp”高度
android:maxHeight =“10dp” 最大高度
android:minHeight =“10dp” 最小高度
android:width =“20dp” 宽度
android:maxWidth=“20dp” 最大宽度
android:minWidth =“20dp” 最小宽度
android:selectAllOnFocus=“true” 获取焦点时选中所有内容
android:shadowColor=“#ff0099” 指定文本阴影的颜色
android:shadowDx=“2” 设置阴影横向坐标开始位置。
android:shadowDy=“2” 设置阴影纵向坐标开始位置。
android:shadowRadius=“3” 设置阴影的半径(一般设置为3)
android:ems=“3” 设置宽度(单位字符)
android:maxems =“3”设置最长宽度
android:minems=“3” 设置最短宽度
android:maxLength=5
android:translationX=“100dp” X轴方向移动
android:translationY=“100dp” Y轴方向移动
android:layout_margin=”50dp”
android:padding=”50dp” 当前View与内部内容距离
android:layout_gravity=“left”
android:gravity=“left”
android:layout_weight=“1”权重(剩余空间按比例划分)
区别:gravity与layout_gravity
gravity控件中的内容相对于控件的对齐方式
layout_gravity控件相对于控件所在容器的对齐方式
在纵向线性布局中,只有左中右有效,上中下无效
在横向线性布局中,只有上中下有效,左中右无效
注意:
match_parent把界面上剩下的空间占满,它前面的没影响,它后面的在当前页面显示不出来
layout_weight:权重,把剩余空间平分,建议写法layout_width=“0dp”
解释:3个Button水平占一行,如果layout_width都是wrap_content那么字符数多的全部显示,并且占的要宽,把剩余的空间平分;
如果layout_width都是match_parent那么各占1/3。
假设一个屏幕宽度是n,3个button各占n,即3n,那么
剩余空间=屏幕宽度n - 控件的总宽度3n = -2n,
平均分成3份即-2n/3
这样,每个控件实际占的是n-2n/3=n/3
也就是说平均分配屏幕宽度
3.2.2 EditText常用监听
setOnTouchListener触摸监听
setOnClickListener 单击监听(对于不可点击View可以添加android:clickable=“true”)
setOnDragListener 拖拽监听
setOnEditorActionListener编辑监听
setOnFocusChangeListener焦点改变监听
setOnKeyListener (View与Activity)按键监听
addTextChangedListener(new TextWatcher(){})文本内容改变监听
setOnLongClickListener长按监听
setOnCreateContextMenuListener创建上下文菜单监听
beforeTextChanged()
onTextChanged()
作用在TextView上,给它注册一个上下文菜单
textview.setOnCreateContextMenuListener();
getMenuInflater().inflate(R.id.main,menu);
4. Menu菜单
ContextMenu上下文菜单:
PopupMenu弹出菜单:
OptionsMenu选项菜单
@Override
publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@Override
publicboolean onOptionsItemSelected(MenuItem item) {
intid = item.getItemId();
if (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
5. 单选RadioButton与多选CheckBox
注意:每一个Layout都要指定它的宽和高,否则报错
(1)获取单选、多选状态:
单选:1.for循环;2.单选按钮组
多选:for循环
privateint[] ids = { R.id.a, R.id.b, R.id.c };
private CheckBox[] cbs = new CheckBox[ids.length];
private RadioButton rb1;
private RadioGroup group;
rb = (RadioButton) this.findViewById(R.id.man);
group = (RadioGroup) this.findViewById(R.id.group);
intcheckedId = group.getCheckedRadioButtonId();
RadioButton rb2 = (RadioButton) this.findViewById(checkedId);
//填充cbs中三个多选按钮
for (inti = 0; i < ids.length; i++) {
cbs[i] = (CheckBox) this.findViewById(ids[i]);
System.out.println(cbs[i].getText().toString() + " " + cbs[i].isChecked());
}
运行截图:
2. 区别:单击监听与状态改变监听
单击监听:只有单击才会触发
状态改变并不一定要单击,也可以通过程序改变
单击监听:
rb = (RadioButton) this.findViewById(R.id.man);
rb.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Toast.makeText(MainActivity.this, rb.getText().toString(), 0).show();
状态改变监听:
rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
publicvoid onCheckedChanged(CompoundButton buttonView, booleanisChecked) {
RadioButton rb = (RadioButton) buttonView;
Toast.makeText(MainActivity.this, rb.getText().toString() + " " + isChecked, 0).show();
if (!isChecked) {
// 通过循环让所有多选按钮取消选中状态
for (CheckBox cb : cbs) {
cb.setChecked(false);
}
}
}
});