TextView文本框

  先介绍几个单位:

  • dp(dip):device independent pixels,译为设备独立像素。
  • pxpixels,译为像素。
  • ptpoint是一个标准的长度单位,1pt1/72英寸。
  • spscaled pixels,译为放大像素。

基础属性详解

  通过下面这个简单的界面,来了解几个最基本的属性:

<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:background="#8fffad"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:gravity="center"
        android:text="TextView(显示框)"
        android:textColor="#EA5246"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

</RelativeLayout>

在这里插入图片描述

上面的TextView中有下述几个属性:

  • id:为TextView设置一个组件id。根据id,我们可以在Java代码中通过findViewById的方法获取到该对象,然后进行相关属性的设置;或者使用RelativeLayout时,参考组件用的也是id
  • layout_width:组件的宽度,一般写wrap_content或者match_parent(fill_parent),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器。当然也可以设置成特定的大小,比如这里为了显示效果,设置成了200dp
  • layout_height:组件的宽度,内容同上。
  • gravity:设置控件中内容的对齐方向,TextView是对齐文字,ImageView是对齐图片。
  • text:设置显示的文本内容。一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容,这里为了方便直接就写到该属性里,不建议这样写。
  • textColor:设置字体颜色,一般通过colors.xml资源来引用。
  • textStyle:设置字体风格,三个可选值:normal(无效果)、bold(加粗)、italic(斜体)。
  • textSize:字体大小,单位一般是用sp
  • background:控件的背景颜色,可以理解为填充整个控件的颜色,也可以是图片。

实际开发例子

带阴影的TextView

  涉及到的几个属性:

  • android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用。
  • android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
  • android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置。
  • android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置。
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:shadowColor="#F9F900"
    android:shadowDx="10.0"
    android:shadowDy="10.0"
    android:shadowRadius="3.0"
    android:text="带阴影的TextView"
    android:textColor="#4A4AFF"
    android:textSize="30sp" />

在这里插入图片描述

带边框的TextView

  如果你想为TextView设置一个边框背景(普通矩形边框或者圆角边框),下面可能帮到你。另外TextView是很多其他控件的父类(比如Button),也可以设置这样的边框。实现原理很简单,编写一个ShapeDrawable的资源文件,然后TextViewbackground设置为这个drawable资源即可。
  简单说一下shapeDrawable资源文件的几个节点以及属性:

  • <solid android:color = "xxx">:这个是设置背景颜色的。
  • <stroke android:width = "xdp" android:color="xxx">:这个是设置边框的粗细以及边框颜色的。
  • <padding androidLbottom = "xdp">:这个是设置边距的。
  • <corners android:topLeftRadius="10px">:这个是设置圆角的。
  • <gradient>:这个是设置渐变色的,可选属性如下:
  1. startColor:起始颜色。
  2. endColor:结束颜色。
  3. centerColor:中间颜色。
  4. angle:方向角度,等于0是从左到右,然后逆时针方向转;等于90度时,从下往上。
  5. type:设置渐变的类型。

  在Drawable目录下新建文件txt_rectborder.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 渐变 -->
    <gradient
        android:angle="270"
        android:endColor="#C0C0C0"
        android:startColor="#FCD209" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

Drawable目录下新建文件txt_radiuborder.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 设置透明背景色 -->
    <solid android:color="#87CEEB" />
    <!-- 设置一个黑色边框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 设置四个圆角的半径 -->
    <corners
        android:bottomLeftRadius="10px"
        android:bottomRightRadius="10px"
        android:topLeftRadius="10px"
        android:topRightRadius="10px" />
    <!-- 设置一下边距,让空间大一点 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

TextViewbackground属性设置成上面这两个Drawable

<LinearLayout 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:background="#FFFFFF"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:background="@drawable/txt_rectborder"
        android:gravity="center"
        android:text="矩形边框的TextView"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtTwo"
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/txt_radiuborder"
        android:gravity="center"
        android:text="圆角边框的TextView"
        android:textSize="18sp" />
</LinearLayout>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值