安卓 TextView

本文详细介绍了Android中的TextView组件,包括layout_width、layout_height、id、text、textColor、textSize、background和gravity等属性的使用。同时,重点讲解了如何为TextView添加阴影效果,通过设置shadowColor、shadowRadius、shadowDx和shadowDy属性实现个性化视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TextView

(文本框)

属性

layout_width

控制宽度
属性值

match_parent 和父容器一样
wrap_content 匹配自身大小
数字 后缀 dp eg: 20dp

layout_height

控制高度
属性值 和宽度一样

id

eg:android:id="@+id/tv"
只能是小写
可在java中找到控件
findViewById(R.id.tv)

text

设置TextView中的内容
和java代码中setText的作用一样 , Java代码会覆盖在xml中的设置属性

textColor

设置文字颜色
#00(透明度)00(红色)00(绿色)00(蓝色)

textSize

内容字体大小
单位 sp

background

背景色
颜色或者是图片

gravity

<attr name="gravity">
        <!-- Push object to the top of its container, not changing its size. -->
        <flag name="top" value="0x30" />
        <!-- Push object to the bottom of its container, not changing its size. -->
        <flag name="bottom" value="0x50" />
        <!-- Push object to the left of its container, not changing its size. -->
        <flag name="left" value="0x03" />
        <!-- Push object to the right of its container, not changing its size. -->
        <flag name="right" value="0x05" />
        <!-- Place object in the vertical center of its container, not changing its size. -->
        <flag name="center_vertical" value="0x10" />
        <!-- Grow the vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill_vertical" value="0x70" />
        <!-- Place object in the horizontal center of its container, not changing its size. -->
        <flag name="center_horizontal" value="0x01" />
        <!-- Grow the horizontal size of the object if needed so it completely fills its container. -->
        <flag name="fill_horizontal" value="0x07" />
        <!-- Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. -->
        <flag name="center" value="0x11" />
        <!-- Grow the horizontal and vertical size of the object if needed so it completely fills its container. -->
        <flag name="fill" value="0x77" />
        <!-- Additional option that can be set to have the top and/or bottom edges of
             the child clipped to its container's bounds.
             The clip will be based on the vertical gravity: a top gravity will clip the bottom
             edge, a bottom gravity will clip the top edge, and neither will clip both edges. -->
        <flag name="clip_vertical" value="0x80" />
        <!-- Additional option that can be set to have the left and/or right edges of
             the child clipped to its container's bounds.
             The clip will be based on the horizontal gravity: a left gravity will clip the right
             edge, a right gravity will clip the left edge, and neither will clip both edges. -->
        <flag name="clip_horizontal" value="0x08" />
        <!-- Push object to the beginning of its container, not changing its size. -->
        <flag name="start" value="0x00800003" />
        <!-- Push object to the end of its container, not changing its size. -->
        <flag name="end" value="0x00800005" />
    </attr>

带阴影的TextView

属性
shadowColor :设置阴影颜色
shadowRadius:设置阴影模糊程度
shadowDx:水平偏移
shadowDy:垂直偏移

代码:

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="20dp"
        android:shadowColor="@color/purple_700"
        android:shadowRadius="3.0"
        android:shadowDx="10"
        android:shadowDy="10" />

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值