textView控件
一、功能简介:
TextView可以说是 Android中最简单的一个控件了,你在前面其实也已经和它打过了一 些打交道。它主要用于在界面上显示一段文本信息。
二、创建textView文件:
以下是在layout.xml布局文件中的创建testView空间的一段代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="24sp" android:textColor="#00ff00" android:text="This is TextView" />
</LinearLayout>
属性介绍:
1.1 android:id=”@+id/text_view” 给当前控件定义了 一个唯一标识符
1.2 android:layout_widthh=”match_parent” 指定了 控件的宽度
1.3 android:layout_heighth=”wrap_content” 指定了控件的高度
1.4 android:gravity=”center” 来指定文字的对齐方式。
1.5 android:textSize=”24sp” 指定文字的大小
1.6 android:textColor=”#00ff00” 指定文字的颜色。
……..
1. layout_width与layout_height属性值说明:
Android中所有的控件都具有layout_width和layout_height这两个属性,可选值有三种 match_parent、fill_parent 和 wrap_content,其中 match_parent 和 fill_parent的意义相同,现在官方更加推荐使用 match_parent。
match_parent表示让当前控件
的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。wrap_content表示让 当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。所以 上面的代码就表示让 TextView的宽度和父布局一样宽,也就是手机屏幕的宽度,让 TextView 的高度足够包含住里面的内容就行
2. gravity属性值说明:
可选值有 top、bottom、left、right、center 等 , 可 以 用 “ | ” 来 同 时 指 定 多 个 值 , 这 里 我 们 指 定 的 "center" , 效 果 等 同 于 "center_vertical|center_horizontal",表示文字在垂直和水平方向都居中对齐。
效果:

还没有评论,来说两句吧...