构建用户界面
一 、Android 应用中一些常用的小部件
1. TextView 显示文本信息
<TextView
android:id="@+id/textOne"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:text="TextView(显示框)"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:background="#000000"
android:textSize="18sp" />
id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!
layout_width:组件的宽度,一般写:**wrap_content**或者**match_parent(fill_parent)**,前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。
layout_height:组件的高度,内容同上。
gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便我直接就写到""里,不建议这样写!!!
textColor:设置字体颜色,同上,通过colors.xml资源来引用,别直接这样写!
textStyle:设置字体风格,三个可选值:**normal**(无效果),**bold**(加粗),**italic**(斜体)
textSize:字体大小,单位一般是用sp!
background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!
2. EditText 文本编辑
<EditText
android:id="@+id/editOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TypeHere"
android:textColor="#EA5246"
android:textStyle="bold|italic"
android:textSize="18sp"
android:hint="Type"
android:gravity="center" >
<requestFocus />
</EditText>
android:hint="默认提示文本"
android:textColorHint="#95A1AA"
<requestFocus />
android:minLines="3"
android:maxLines="3"
android:singleLine="true"
android:textScaleX="1.5"
android:textScaleY="1.5"
android:paddingTop = "5dp"
3. Button 普通按钮
<Button
android:id="@+id/btnOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:text="按钮"/>
4. ImageButton 图片按钮
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
5. ToggleButton 开关按钮
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="关闭声音"
android:textOn="打开声音" />
6. RadioButton 单旋钮,选中之后不能取消
<RadioButton
android:id="@+id/btnMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
7. RadioGroup 单选按钮组
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation=