注意事项
- 务必要注意不管是在
XML
还是class
文件中,所有控件首字母都是大写,否则程序会崩的,还有就是标签对的第一个标签结尾是没有/
的,而单标签结尾得有/
- 设置id时,都是
"@+id/自己定的id"
,常用设置的属性有id
、layout_width
、layout_height
、text
常件属性值
(1)fill_parent
:将控制布满整个屏幕,match_parent
跟其使用效果貌似没啥区别
(2)wrap_content
:控件里面的内容有多大,这个控件就有多大
(3)px
:像素,是固定的值
(4)dpi
:就是每英寸占有多少像素,公式:用勾股定理计算像素长和像素宽的斜边,然后除以手机尺寸
(5)dp
:因为不同手机可能像素不同,所以设置一个固定像素的话可能在显示效果上有偏差,比如一个控件设置100像素宽,一个手机宽像素是200,一个是400,那么这个控件在两个手机里就分别占一半和四分之一宽,为了解决这个问题,就有dp,他会根据手机像素的不同来调整做到相同的效果(公式里会根据dpi来计算)
(6)sp
:通常用于指定字体大小,当手机改变字体大小时,其会随之改变,比如给两个文本字体分别设置50sp和50dp,正常情况是一样大的,但到手机设置里将手机字体设置大点,会发现50sp的变大了,50dp的不变
常用控件
1.文本标签: TextView
举例:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/abc" />
2.编辑文本框: EditView
举例:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit"
android:inputType="text"
android:hint="请在此输入用户名..."/> //hint就是灰色的那些提示字
3.按钮: Button
举例:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="click me"/>
4.多选框: CheckBox
举例:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:text="aaa"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:text="bbb"/>
5.单选按钮
RadioButton,要注意的是因为单选按钮需要分组,也就是你得分好哪几个单选按钮之间只能选一个,然后用RadioGroup
标签来圈起来,比如:
<RadioGroup
android:id="@+id/group1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaa"/> //单标签,后面有/
<RadioButton
android:id="@+id/a2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bbb"/>
</RadioGroup>
6.图片: ImageView
要注意的是其一般是通过src
属性引用res-drawable
对应分辨率的文件夹、assets
或网络等地方的图片,比如引用安卓程序默认有的一个图片(就是那个绿色机器人):
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/ic_launcher"/>
当然在class
文件当中也能设置图片,比如:
imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
7.进度条: ProgressBar
有各种风格,垂直风格(一个圈圈),水平风格(一条线的,用的比较多的),SeekBar
(可拖拽的,比如调音量的),RatingBar
(评分几星的那种),设置方式通过style
(前面没有android:),然后值有6种(Horizontal
/Small
/Large
/Inverse
/Small.Inverse
/Large.Inverse
),值的形式是"?android:attr/progressBarStyle"+"类型"
,比如下面是个水平进度条:
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"/>
此时如果要设置其初始值、第二进度(两个进度都在一条线上,但颜色不同等)、加减可以在class
中设置:
progressBar.setMax(100); //设置这条进度条总数值
progressBar.setProgress(20); //设置第一进度初始数值
progressBar.setSecondaryProgress(30); //设置第二进度初始数值
监听器控制加减进度:
class ButtonListener implements OnClickListener{
public void onClick(View v){
progressBar.incrementProgressBy(10); //监听器中每次点击第一进度增加10,如果要减10就-10
progressBar.incrementSecondaryProgressBy(10); //每次点击第二进度就加10
}
}