Android 表单之 TextView(文本框)详解

╰+攻爆jí腚メ 2022-05-11 02:52 356阅读 0赞

目录

TextView( 文本框) 简 介

基础属性

带阴影 TextView


TextView( 文本框) 简 介

TextView(文本框)用于显示文本的一个控件,Android 官方 API 文档链接

先熟悉以下几个单位:

dp(dip): device independent pixels(设备独立像素),不同设备有不同的显示效果,和设备硬件有关,一般为了支持 WVGA、HVGA 和 QVGA 推荐使用这个,不依赖像素。

px: pixels(像素) 不同设备显示效果相同,一般 HVGA 代表 320x480 像素,这个用的比较多。

pt: point 是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用

sp: scaled pixels(放大像素),主要用于字体显示 best for textsize。

基础属性

70 图 1.1

上图 1.1 效果实现代码如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#40A240"
  6. android:gravity="center"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/txtOne"
  10. android:layout_width="200dp"
  11. android:layout_height="200dp"
  12. android:background="#000000"
  13. android:gravity="center"
  14. android:text="TextView(显示框)"
  15. android:textColor="#fff"
  16. android:textSize="18sp"
  17. android:textStyle="bold|italic" />
  18. </RelativeLayout>

< TextView 中属性介绍如下:

id:为 TextView 设置一个组件 id,根据 id,可以在 Java 代码中通过 findViewById(id) 的方法获取到该对象,然后进行相关属性的设置

layout_width:组件的宽度,一般写:wrap_content或者match_parent(fill_parent),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。

layout_height:组件的宽度,内容同上。

gravity:设置控件中内容的对齐方向,TextView 中是文字,ImageView 中是图片等等。

text:设置显示的文本内容,一般是把字符串写到 src/values/string.xml 文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便直接就写到 “” 里

textColor:设置字体颜色,同上,通过 src/valuses/colors.xml 资源来引用,别直接这样写!

textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体),多个样式时,使用 “|” 隔开

textSize:字体大小,单位一般是用 sp!

background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片

带阴影 TextView

阴影涉及到以下几个属性:

android:shadowColor: 设置阴影颜色,需要与 shadowRadius 一起使用

android:shadowRadius: 设置阴影的模糊程度,设为 0.1 就变成字体颜色了,建议使用 3.0

android:shadowDx: 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置

android:shadowDy: 设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

70 1 图 1.2

上图 1.2 效果代码如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#808080"
  6. android:gravity="center"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerInParent="true"
  12. android:shadowColor="#000000"
  13. android:shadowDx="10.0"
  14. android:shadowDy="10.0"
  15. android:shadowRadius="3.0"
  16. android:text="带阴影 TextView"
  17. android:textColor="#FAFAFA"
  18. android:textSize="30sp" />
  19. </RelativeLayout>

发表评论

表情:
评论列表 (有 0 条评论,356人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android组件详解TextView

    这篇博文献给正在android学习路上的弟弟,希望有一天以下内容对你有所帮助。 了解一个类,首先要了解他的结构,尤其是API中的这种类。 先了解下`TextView`的结构