Text View 可以说是Android中最简单的一个控件了。它的主要功能是用于在界面上显示一段文本的信息。假如你在一个界面上看到的“Hello world!! !“。那要怎样才能实现这显示呢?
下面是显示Helloworld的代码:
<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/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello Word!!!" />
</LinearLayout>
用模拟器运行后的结果如下:
大家在界面上看到的效果是由于Text View中的文字默认是左上角对齐的,但是要怎样才能使文字效果居中呢?只需要在上面的Text View代码中加入一句代码:android:gravity=“center”即可。android:gravity是来指定文字的对齐方式,可选择的值有top,bottom,left,right,center。我们这里用center作为例子。运行后的效果如下:
另外我们也可以对Text View中的文字的大小和颜色进行修改。
修改文字的大小代码:android:textSize=”35sp”(sp是表示自他大小的单位)。修改文字的颜色代码:android:textColor=”#00ffff”(这里的#00ffff是表示颜色的十六进制,其他颜色的十六进制可以查询相应的资料,这里就不做解释啦)。
运行模拟器的之后的效果如下:
实现上述效果的全部代码如下:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="35sp"
android:textColor="#00ffff"
android:text="Hello Word!!!" />
----------
如有错误之处,请大家多多包涵并给予建议或指出错误,谢谢!