当前位置:   article > 正文

Android TextView详解(一)

android textview

TextView简介

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

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

普通TextView的使用

  1. <!--text:用于设置显示文本,文本通常先在values/strings.xml中定义-->
  2. <!--textColor:用于设置文本颜色,颜色通常先在values/colors.xml中定义-->
  3. <!--textSize:用于设置文字大小,单位通常为sp-->
  4. <TextView
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:layout_marginTop="20dp"
  8. android:text="普通文本"
  9. android:textColor="#0000ff"
  10. 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设置的属性文件不同。布局如下:

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

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

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

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

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

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

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

文字上方设置图片

  1. <TextView
  2. android:id="@+id/textView5"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="20dp"
  6. android:drawableTop="@android:drawable/sym_def_app_icon"
  7. android:padding="5dp"
  8. android:text="文字上方设置图片"
  9. android:textColor="#000000" />

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

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

可跳转到电话:15894483722

  1. <TextView
  2. android:id="@+id/textView7"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="20dp"
  6. android:autoLink="phone"
  7. android:padding="5dp"
  8. android:text="可跳转到电话:15894483722"
  9. android:textColor="#000000" />

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

android:autoLink

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

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

email:匹配电子邮件地址。

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

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系列

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/984599
推荐阅读
相关标签
  

闽ICP备14008679号