TextView的使用

来源:互联网 发布:微信支付退款接口 php 编辑:程序博客网 时间:2024/06/07 07:09

TextView的主要作用就是显示一段文本信息
首先创建一个UI项目:
activity _main.xml中如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.uiwidgettest.MainActivity">    <TextView        android:id="@+id/text_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="This is TextView"/></RelativeLayout>

android:id—为当前控件定义一个唯一的标识符
android:layout_width—指定控件的宽度
android:layout_heigth—指定控件的高度

一般来说,Android中的控件都具有2种属性,它们的可选值有3种,下面我们来了解一下它们。
match_parent : 让当前控件的大小和父布局的大小一样。
wrap_content : 控件内容决定当前控件的大小
fill_parent : 与match_parent意义相同,目前官方更推荐后者。

当然除过以上的,也可自行对控件的宽和高进行指定控制(在这种方法中要注意,宽高与手机屏幕的适配)。

了解过后,运行程序。 效果如下:
这里写图片描述

现在修改一下代码:


<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is TextView"/>
这里写图片描述

可以发现,android:gravity是用来指定文字的对齐方式,可选的值有很多,并且同时可以用“|”来指定多个值,上段代码中的center表示将对象居中对齐,也可以显示出TextView的宽度与屏幕是一样的。
TextView还有其他的属性,在需要用的时候可以去查询相关书籍。

0 0