[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
}
}