Android的TextView基本属性
一、 说明
本文主要讲一些Android的TextView的一些基本的属性。
二、 所用工具
Android Studio
三、 TextView的基本属性说明
1. id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,使用RelativeLayout(相对布局)时,参考组件要使用id!
2. layout_width:组件的宽度
可选值:
"wrap_content ",控件显示的内容多大,控件就多大。
"match_parent(fill_parent) ",填满该控件所在的父容器;
也可以设置成特定的大小,设置成了300dp等等。
3. layout_height:组件的高度
可选值:
"wrap_content ",控件显示的内容多大,控件就多大。
"match_parent(fill_parent) ",填满该控件所在的父容器;
也可以设置成特定的大小,设置成了300dp等等。
4. gravity:设置控件中内容的对齐方向
常用值:
center:居中对齐
top:顶部对齐
buttom:底部对齐
left:左边对齐
right:右边对齐
5. textAlignment:设置字体的对齐方向
常用值:
Center:居中对齐
textStart:左边对齐
textEnd:右边对齐
6. text:设置显示的文本内容,一般我们是把字符串写到strings.xml文件中,然后通过@String/xxx取得对应的字符串内容的
7. textColor:设置字体颜色,一般我们是把字符串写到colors.xml文件中,然后通过@color/xxx取得对应的颜色的
8. textStyle:设置字体风格
可选值:
normal(无效果)
bold(加粗)
italic(斜体)
9. textSize:设置字体大小!
10. background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片!
四、 代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="测试页面"
android:textSize="50sp"
android:textColor="#000000"
android:background="#ffffff"
android:gravity="center"
android:textStyle="bold"
android:textAlignment="center">
</TextView>
</LinearLayout>
</LinearLayout>
五、 效果图