Textview的学习

本文详细介绍了Android中TextView的各类属性,包括文本颜色、大小、风格、阴影效果、跑马灯显示等,并通过实例展示了如何在XML布局中设置这些属性。同时,还讨论了如何使TextView在焦点时自动启动跑马灯效果,以及如何自定义TextView使其默认获取焦点。此外,还提到了如何在Java代码中通过id找到TextView并进行内容修改。

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

TextView的属性

android:textColor 文本颜色

android:textColorHighlight 文本高亮颜色

android:textColorHint 文本提示颜色

android:textColorLink 链接文本颜色

android:textIsSelectable 文本能够被选中

android:textScaleX 水平缩放参数

android:textSize 文本大小

android:textStyle 文本风格

android:typeface 文本字体

android:width 文本宽度

android:autoLink 是否自动链接网址或邮箱地址

android:autoText 自动检测错误

android:bufferType 决定getText()返回的类型

android:capitalize 指定使用类型

android:cursorVisible 光标是否可见

android:digits 数字输入

android:drawableBottom 内容显示在文字的下边

android:drawableEnd 内容显示在文字的结尾

android:drawableLeft 内容显示在文字的左边

android:drawablePadding 内容和文字之间的空隙

android:drawableRight 内容显示在文字的右边

android:drawableStart 内容显示在文字的开始

android:drawableTop 内容显示在文字的上边

android:editable 编辑功能,能够使用输入法

android:editorExtras 编辑功能扩展,用户设置

android:ellipsize 椭圆区域的显示方式

android:ems 可以在更多系统上运行

android:fontFamily 字体风格

android:freezesText 冻结在光标位置

android:gravity 文字小于显示范围时,x和y轴方向的调整

android:height 文字像素高度

android:hint 文本空白时的提示语

android:imeActionId 激活输入法ID序号

android:imeActionLabel 激活输入法符号

android:imeOptions 输入法操作

android:includeFontPadding 足够空间容纳字体显示

android:inputMethod 指定输入法

android:inputType 选择输入法

android:lineSpacingExtra 额外的文字间距

android:lineSpacingMultiplier 额外的文字间距,乘数

android:lines 多行显示

android:linksClickable 点击链接

android:marqueeRepeatLimit 跑马灯重复限制

android:maxEms 最大系统兼容

android:maxHeight 最大文本高度

android:maxLength 最大文本长度

android:maxLines 最大文本行数

android:maxWidth 最大文本长度

android:minEms 最小系统兼容

android:minHeight 最小文本高度

android:minLines 最小文本行数

android:minWidth 最小文本宽度

android:numeric 支持数字输入

android:password 文本作为密码

android:phoneNumber 支持电话号码输入

android:privateImeOptions 私人输入操作

android:selectAllOnFocus 点击全部选中

android:shadowColor 阴影颜色

android:shadowDx 阴影水平偏移

android:shadowDy 阴影垂直偏移

android:shadowRadius 阴影半径

android:singleLine 单行显示

android:text 显示文本

android:textAllCaps 文本全部大写

android:textAppearance 基本的文字颜色,字体,大小,风格

1.基础属性详解:
通过下面这个简单的界面,我们来了解几个最基本的属性:

在这里插入图片描述
布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="259dp"
        android:gravity="center"
        android:shadowColor="@color/red"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:singleLine="true"
        android:text="@string/tv_one"
        android:textColor="@color/black"
        android:textSize="40sp"
        android:textStyle="italic"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true"/>
</LinearLayout>

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

id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!
TextView tv_one = findViewById(R.id.tv_one);
tv_one.setText(“WQQ”);//这里可以改变text的内容。
layout_width:组件的宽度,一般写:wrap_content或者match_parent,前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。
layout_height:组件的宽度,内容同上。
gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便我直接就写到""里,不建议这样写!!!
textColor:设置字体颜色,同上,通过colors.xml资源来引用,别直接这样写!
textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize:字体大小,单位一般是用sp!
background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!
shadowColor :阴影颜色
shadowDx :阴影水平偏移
shadowDy :阴影垂直偏移
shadowRadius :阴影半径
singleLine :单行显示
android:ellipsize=“marquee” 表示跑马灯显示,ellipsize表示在哪里省略文本
marqueeRepeatLimit="marquee_forever"表示一直循环显示
focusableInTouchMode=“true” 用于控制视图在触摸模式下是否可以聚焦。
focusable=“true” 表示是否获取焦点
android:clickable="true"表示是否支持点击
注意:这里通过点击来获取焦点,开始跑马运动。另一种方法我们可以让它默认起来就跑马运行。

//创建一个mytextview的类
public class mytextview extends TextView {
    public mytextview(Context context) {
        super(context);
    }

    public mytextview(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public mytextview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

//修改textvie,用我们上面自定义的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.example.activity0726.mytextview
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="259dp"
        android:gravity="center"
        android:shadowColor="@color/red"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:singleLine="true"
        android:text="@string/tv_one"
        android:textColor="@color/black"
        android:textSize="40sp"
        android:textStyle="italic"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:clickable="true" />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值