Android TextView详解(一)

TextView简介

在Android应用中,我们通常使用TextView向用户展示文本信息,并可设置文字的字体大小、颜色、背景色等基本样式,本篇我们将学习TextView的一些常用操作和属性。

下面是使用TextView实现的一些效果,我们接下来看实现方式。

普通TextView的使用

<!--text:用于设置显示文本,文本通常先在values/strings.xml中定义-->
<!--textColor:用于设置文本颜色,颜色通常先在values/colors.xml中定义-->
<!--textSize:用于设置文字大小,单位通常为sp-->
<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginTop="20dp"
     android:text="普通文本"
     android:textColor="#0000ff"
     android:textSize="14sp" />

这里需要注意两个基本属性layout_width和layout_height,分别表示TextView的宽度和高度设置。 

layout_width和layout_height可以填入wrap_content,match_parent或者具体的数值。

  • wrap_content宽高自适应文字内容长度,文字越长,它的宽度越宽,直到父容器允许的最大宽/高度。
  • match_parent宽高撑满父容器。
  • 输入具体数值比如100dp,单位为dp。 

TextView加边框、背景色和渐变背景色都是通过设置background属性实现,三种情况拥有相同的布局结构,唯一不同的就是background设置的属性文件不同。布局如下:

<TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:background="@drawable/jianbian"
            android:padding="5dp"
            android:text="带渐变色背景和边框的文本"
            android:textColor="#ffffff" />

带边框的TextView的background属性使用的drawable文件border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"></corners>
    <stroke android:width="1dp" android:color="#ff0000"></stroke>
</shape>

带背景色的TextView的background属性使用的drawable文件bg_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ff0000"></solid>
    <corners android:radius="5dp"></corners>
</shape>

带渐变色背景和边框的TextView的background属性使用的drawable文件jianbian.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--corners设置圆角,radius为4个角设置相同的圆角,还可以设置单独的圆角,使用topLeftRadius、topRightRadius等-->
    <corners android:radius="5dp"></corners>
    <!--stroke设置边框,width为边框宽度,color边框颜色-->
    <stroke android:width="1dp" android:color="#ff0000"></stroke>
    <!--gradient设置渐变色,startColor为开始颜色,endColor为结束颜色,type为渐变类型,此处为线性渐变-->
    <gradient android:startColor="#59cde9" android:endColor="#0a2a88" android:type="linear"></gradient>
    <!--solid为填充色,即背景色,-->
    <!--<solid android:color="#ff0000"></solid>-->
</shape>

文字上方设置图片

<TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:drawableTop="@android:drawable/sym_def_app_icon"
            android:padding="5dp"
            android:text="文字上方设置图片"
            android:textColor="#000000" />

可跳转到网页:https://www.baidu.com/

<!--autoLink:是否自动查找url、邮件地址等链接并将其转换为可点击的链接。默认值为"none",禁用此功能。
        可取值如下:
        all:匹配所有模式(相当于 web|email|phone|map)。
        email:匹配电子邮件地址。
        map:匹配地图地址。弃用:
        none:不匹配任何模式(默认)。
        phone:匹配电话号码。
        web:匹配 Web URL
        -->
        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:autoLink="web"
            android:padding="5dp"
            android:text="可跳转到网页:https://www.baidu.com/"
            android:textColor="#000000" />

可跳转到电话:15894483722

<TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:autoLink="phone"
            android:padding="5dp"
            android:text="可跳转到电话:15894483722"
            android:textColor="#000000" />

以上为示例程序所展示的效果的代码实现,下面总结一下TextView常用的一些属性:

android:autoLink

autoLink:是否自动查找url、邮件地址等链接并将其转换为可点击的链接。默认值为"none",禁用此功能。 可取值如下:

all:匹配所有模式(相当于 web|email|phone|map)。

email:匹配电子邮件地址。

map:匹配地图地址。弃用。

扫描二维码关注公众号,回复: 14814335 查看本文章

none:不匹配任何模式(默认)。

phone:匹配电话号码。

web:匹配 Web URL。

android:drawableTop、android:drawableBottom系列

可在文本的不同方向上绘制显示对象。

android:gravity

文本小于视图长度时的对齐方式,可设置居中、居左、居右等。

android:singleLine

始终显示成单行文本。

android:textColor

设置文本颜色。

android:text

设置要显示的文本。

android:textSize

设置字体大小,单位通常是sp。

android:layout_margin系列

设置外边距,一般用于设置与其他元素的间距。

android:layout_padding系列

设置内边距,当设置了边框、背景色等,一般需要设置内边距,否则,显示出来的效果会比较丑。

原创不易,点个赞再走吧。

猜你喜欢

转载自blog.csdn.net/qq_34215018/article/details/127529247