前言
TextView 是什么?
向用户显示文本,并可选择允许他们编辑文本。TextView 是一个完整的文本编辑器,但是基类为不允许编辑;其子类 EditText 允许文本编辑。
解释说明:
-
Button:用户可以点击或单击以执行操作的用户界面元素。
-
CheckedTextView:TextView支持Checkable界面和显示的扩展。
-
Chronometer:实现简单计时器的类。
-
DigitalClock:API17已弃用可用TextClock替代。
-
EditText:用于输入和修改文本的用户界面元素。
-
TextClock:可以将当前日期和/或时间显示为格式化字符串。
使用
1. 常用属性
效果图
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dimen_20"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/str_setting_color_size"
android:layout_marginTop="@dimen/dimen_10"
android:textColor="@color/color_ff0000"
android:textSize="@dimen/text_size_20" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@mipmap/ic_launcher"
android:layout_marginTop="@dimen/dimen_10"
android:gravity="center_vertical"
android:shadowColor="@color/color_FF773D"
android:shadowDx="30"
android:shadowDy="-20"
android:shadowRadius="2"
android:text="右侧添加图片和使用阴影"
android:textColor="@color/color_188FFF"
android:textSize="@dimen/text_size_20" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="email|phone"
android:gravity="center_vertical"
android:layout_marginTop="@dimen/dimen_10"
android:text="可点击跳转邮件:[email protected]\n可点击跳转电话:0215201314"
android:textColor="@color/color_188FFF"
android:textSize="@dimen/text_size_14" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lineSpacingMultiplier="1.2"
android:layout_marginTop="@dimen/dimen_10"
android:maxLength="2"
android:text="TxtView继承了View,它还是Button、EditText两个UI组件类的父类。它的作用是在用户界面上显示文本素。从功能上来看TextView就是个文本编辑器,只不过Android关闭的它的可编辑功能。如果需要一个可编辑的文本框,就要使用到它的子类Editext了,Editext允许用户编辑文本框中的内容。TextView和Editext它俩最大的区别就在于TextView不允许用户编辑文本内容,Editext允许用户编辑文本内容。
下面咱写几个实例来详细了解一下TextView的。"
android:textColor="@color/color_188FFF"
android:textSize="@dimen/text_size_14" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/color_ff0000"
android:layout_marginTop="@dimen/dimen_10"
android:padding="10dp"
android:text="背景色红色的文本"
android:textColor="@color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_10"
android:background="@drawable/bg_tv_frame_red"
android:padding="10dp"
android:text="带着红色边框的文本" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_10"
android:background="@drawable/bg_tv_frame_gradient"
android:padding="10dp"
android:textColor="@color/white"
android:text="带着边框和背景色渐变的文本" />
</LinearLayout>
- background 设置边框的文件
android:background="@drawable/bg_tv_frame_red"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--radius四个圆角统一设置,也可以单独对某一个圆角设置。例:topLeftRadius-->
<corners android:radius="2dp"/>
<!--边框宽度width、颜色color-->
<stroke android:width="4px" android:color="@color/color_ff0000" />
</shape>
- 带着边框和背景色渐变
android:background="@drawable/bg_tv_frame_gradient"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--radius四个圆角统一设置,也可以单独对某一个圆角设置。例:topLeftRadius-->
<corners android:radius="8dp"/>
<!--边框宽度width、颜色color-->
<stroke android:width="1dp" android:color="@color/color_ff0000" />
<!--渐变的颜色设置开始到结束-->
<gradient
android:startColor="@color/color_188FFF"
android:centerColor="@color/color_FF773D"
android:endColor="@color/color_ff0000"
android:type="linear"
/>
</shape>
2. 给文本添加下划线
效果图
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dimen_20"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"