Android TextView 使用以及属性(方法)大全,2024移动开发者真的太难了

  • DigitalClock:API17已弃用可用TextClock替代。

  • EditText:用于输入和修改文本的用户界面元素。

  • TextClock:可以将当前日期和/或时间显示为格式化字符串。

看看他的儿子都这么牛掰,何况是爸爸,今天咱就看看这个爸爸级组件:TextView

使用TextView


1.在xml中创建并设置属性

咱们看上图说话。上图的文字显示多种多样,但是也仅包含TextView的部分功能,看看这多种多样的显示也是比较有意思的。

下面咱看看代码实践:

<?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:layout_margin=“@dimen/dimen_20”

android:orientation=“vertical”>

<TextView

android:id=“@+id/textView”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“TextView” />

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“@string/str_setting_color_size”

android:layout_marginTop=“@dimen/dimen_10”

android:textColor=“@color/color_ff0000”

android:textSize=“@dimen/text_size_20” />

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:drawableLeft=“@mipmap/ic_launcher”

android:layout_marginTop=“@dimen/dimen_10”

android:gravity=“center_vertical”

android:shadowColor=“@color/color_FF773D”

android:shadowDx=“30”

android:shadowDy=“-20”

android:shadowRadius=“2”

android:text=“右侧添加图片和使用阴影”

android:textColor=“@color/color_188FFF”

android:textSize=“@dimen/text_size_20” />

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:autoLink=“email|phone”

android:gravity=“center_vertical”

android:layout_marginTop=“@dimen/dimen_10”

android:text=“可点击跳转邮件:SCC5201314@qq.com\n可点击跳转电话:0215201314”

android:textColor=“@color/color_188FFF”

android:textSize=“@dimen/text_size_14” />

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:ellipsize=“end”

android:gravity=“center_vertical”

android:lineSpacingMultiplier=“1.2”

android:layout_marginTop=“@dimen/dimen_10”

android:maxLength=“2”

android:text="TxtView继承了View,它还是Button、EditText两个UI组件类的父类。它的作用是在用户界面上显示文本素。从功能上来看TextView就是个文本编辑器,只不过Android关闭的它的可编辑功能。如果需要一个可编辑的文本框,就要使用到它的子类Editext了,Editext允许用户编辑文本框中的内容。TextView和Editext它俩最大的区别就在于TextView不允许用户编辑文本内容,Editext允许用户编辑文本内容。

下面咱写几个实例来详细了解一下TextView的。"

android:textColor=“@color/color_188FFF”

android:textSize=“@dimen/text_size_14” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“@color/color_ff0000”

android:layout_marginTop=“@dimen/dimen_10”

android:padding=“10dp”

android:text=“背景色红色的文本”

android:textColor=“@color/white” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/dimen_10”

android:background=“@drawable/bg_tv_frame_red”

android:padding=“10dp”

android:text=“带着红色边框的文本” />

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/dimen_10”

android:background=“@drawable/bg_tv_frame_gradient”

android:padding=“10dp”

android:textColor=“@color/white”

android:text=“带着边框和背景色渐变的文本” />

background设置边框的文件 android:background=“@drawable/bg_tv_frame_red”

<?xml version="1.0" encoding="utf-8"?>

带着边框和背景色渐变 android:background=“@drawable/bg_tv_frame_gradient”

<?xml version="1.0" encoding="utf-8"?>

<gradient

android:startColor=“@color/color_188FFF”

android:centerColor=“@color/color_FF773D”

android:endColor=“@color/color_ff0000”

android:type=“linear”

/>

2.在xml中创建,在代码中设置属性

效果图:

布局文件 :

<?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:layout_margin=“@dimen/dimen_20”

android:orientation=“vertical”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“下面是用代码实现效果”

android:textSize=“@dimen/text_size_18”

android:layout_marginTop=“@dimen/dimen_20”

android:layout_marginBottom=“@dimen/dimen_10”

android:textColor=“@color/black”

android:textStyle=“bold” />

<TextView

android:id=“@+id/tv_flag”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:textColor=“@color/color_188FFF”

android:layout_marginTop=“@dimen/dimen_10”

android:text=“给文本加划线”

android:textSize=“@dimen/text_size_18” />

<TextView

android:id=“@+id/tv_gradient”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/dimen_10”

android:textColor=“@color/white”

android:text=“文字渐变是不是很神奇”

android:textSize=“@dimen/text_size_18” />

<TextView

android:id=“@+id/tv_bg”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/dimen_10”

android:padding=“10dp”

android:text=“设置背景色”

android:textColor=“@color/white”

android:textSize=“@dimen/text_size_18” />

<TextView

android:id=“@+id/tv_size”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_marginTop=“@dimen/dimen_10”

android:textColor=“@color/color_ff0000”

android:text=“文字特别大小不一致” />

<TextView

android:id=“@+id/tv_onclick”

android:layout_width=“match_parent”

android:layout_marginTop=“@dimen/dimen_10”

android:layout_height=“wrap_content”

android:textSize=“@dimen/dimen_20”

android:text=“可点击可长按” />

运行结果:

在代码中实现:

//下划线并加清晰

tv_flag.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

tv_flag.getPaint().setAntiAlias(true);//抗锯齿

int[] colors = {0xff188fff, 0xffff773D, 0xffff0000};//颜色的数组

LinearGradient mLinearGradient = new LinearGradient(0, 0, 0,

tv_gradient.getPaint().getTextSize(), colors, null, Shader.TileMode.CLAMP);

tv_gradient.getPaint().setShader(mLinearGradient);

tv_gradient.invalidate();

int fillColor = Color.parseColor(“#ff0000”);//内部填充颜色

GradientDrawable gd = new GradientDrawable();//创建drawable

gd.setColor(fillColor);//设置背景色

gd.setCornerRadius(10);//设置圆角

tv_bg.setBackground(gd);//设置背景

Spannable wordtoSpan = new SpannableString(tv_size.getText().toString());

//setSpan:参数1,设置文字大小;参数2,开始的文字位置;参数3,结束改变文字位置不包含这个位置

wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 18)), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 24)), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 10)), 5, tv_size.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv_size.setText(wordtoSpan);

//TextView其实也是有点击事件的毕竟它的爸爸Veiew

tv_onclick.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

MLog.e(“这里是点击事件”);

Toast.makeText(TextViewActivity.this,“这里是点击事件”,Toast.LENGTH_SHORT).show();

}

});

tv_onclick.setOnLongClickListener(new View.OnLongClickListener() {

@Override

public boolean onLongClick(View v) {

MLog.e(“这里长按事件”);

Toast.makeText(TextViewActivity.this,“这里长按事件”,Toast.LENGTH_SHORT).show();

//true表示事件已消费

return true;

}

});

运行结果分析

  • TextView的属性在xml中可以使用的大部分在代码中也是可以实现的,看个人喜好怎么去使用。

  • 因TextView继承View,所以可以使用View的方法。如View.OnClickListener()和View.OnLongClickListener()还有去慢慢探索吧。

3.在代码中创建并设置属性

先看效果图:

下面是实现所用的代码:

//ll_act_tv布局文件根布局id

LinearLayout ll_act_tv = findViewById(R.id.ll_act_tv);

TextView textView = new TextView(this);//创建控件

textView.setText(“蠢代码写的哦”);//设置控件内容

textView.setTextColor(Color.RED);//设置控件颜色

textView.setTextSize(DensityUtil.dip2px(this, 20));//设置控件字体大小

ll_act_tv.addView(textView);

TextView今天就聊到这里,后面还有它的子类,比较子类也是比较厉害的不可能一文搞定。你学会了吗?嘿嘿嘿

TextViewXML属性和相关方法说明。

android:allowUndo

是否应允许对可编辑文本进行撤消。默认为真。

可以是布尔值,例如 “true"或"false”。

android:autoLink

控制是否自动找到 url 和电子邮件地址等链接并将其转换为可点击的链接。默认值为"none",禁用此功能。

必须是以下常量值中的一个或多个(以"|"分隔)。

Constant

Value

Description

all

f

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

email

2

匹配电子邮件地址。

map

8

匹配地图地址。弃用:见。Linkify.MAP_ADDRESSES

none

0

不匹配任何模式(默认)。

phone

4

匹配电话号码。

web

1

匹配 Web URL。

相关方法:setAutoLinkMask(int)

android:autoSizeMaxTextSize

自动调整文本大小时要使用的最大文本大小约束。

可以是一个维度值,它是一个浮点数,附加一个单位,例如" 14.5sp"。可用单位有:px(像素)、dp(与密度无关的像素)、sp(基于首选字体大小的缩放像素)、in(英寸)和 mm(毫米)。

相关方法:setAutoSizeTextTypeUniformWithConfiguration(int,int,int,int)

android:autoSizeMinTextSize

自动调整文本大小时要使用的最小文本大小约束。

相关方法:setAutoSizeTextTypeUniformWithConfiguration(int,int,int,int)

android:autoSizePresetSizes

要与autoSizeTextTypeset to结合使用的维度的资源数组 uniform。autoSizeStepGranularity如果设置则覆盖 。

相关方法:setAutoSizeTextTypeUniformWithPresetSizes(int,int)

android:autoSizeStepGranularity

如果autoSizeTextType设置为"uniform",请指定自动调整步长。默认值为1px。覆盖autoSizePresetSizes(如果设置)。

相关方法:setAutoSizeTextTypeUniformWithConfiguration(int,int,int,int)

android:autoSizeTextType

指定自动调整大小的类型。请注意,此功能不受EditText支持,仅适用于 TextView。

必须是以下常量值之一。

Constant

Value

Description

none

0

没有自动调整大小(默认)。

uniform

1

统一的水平和垂直文本大小缩放以适应容器。

相关方法:setAutoSizeTextTypeWithDefaults(int)

android:autoText

如果设置,则指定此 TextView 具有文本输入法并自动更正一些常见的拼写错误。默认值为"假"。

相关方法:setKeyListener(KeyListener)

android:breakStrategy

中断策略(控制段落布局)。

必须是以下常量值之一。

Constant

Value

Description

balanced

2

断线策略平衡线长度。

high_quality

1

换行使用高质量的策略,包括连字符。

simple

0

换行使用简单的策略。

相关方法:setBreakStrategy(int)

android:bufferType

确定 getText() 将返回的最小类型。默认值为"正常"。请注意,EditText 和 LogTextBox 始终返回 Editable,即使您在此处指定的功能不那么强大。

必须是以下常量值之一。

Constant

Value

Description

editable

2

只能返回 Spannable 和 Editable。

normal

0

可以返回任何 CharSequence,如果源文本是 Spanned,则可以是 Spanned 。

spannable

1

只能返回 Spannable。

相关方法:setText(int,TextView.BufferType)

android:capitalize

如果设置,则指定此 TextView 具有文本输入法并应自动将用户键入的内容大写。默认值为"无"。

必须是以下常量值之一。

Constant

Value

Description

characters

3

大写每个字符。

none

0

不要自动大写任何东西。

sentences

1

将每个句子的第一个单词大写。

words

2

将每个单词的第一个字母大写。

相关方法:setKeyListener(KeyListener)

android:cursorVisible

使光标可见(默认)或不可见。

相关方法:setCursorVisible(boolean)

android:digits

如果设置,则指定此 TextView 具有数字输入法,并且这些特定字符是它将接受的字符。如果设置了此项,则暗示数字为真。默认值为假。

相关方法:setKeyListener(KeyListener)

android:drawableBottom

要在文本下方绘制的可绘制对象。

相关方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)

android:drawableEnd

要绘制到文本末尾的可绘制对象。

相关方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)

android:drawableLeft

要绘制到文本左侧的可绘制对象。

相关方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)

android:drawablePadding

可绘制对象和文本之间的填充。

相关方法:setCompoundDrawablePadding(int)

android:drawableRight

相关方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)

android:drawableStart

要绘制到文本开头的可绘制对象。

相关方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)

android:drawableTint

应用于复合(左侧、顶部等)可绘制对象的色调。

相关方法:setCompoundDrawableTintList(ColorStateList)

android:drawableTintMode

用于应用复合(左侧、顶部等)可绘制色调的混合模式。

相关方法:setCompoundDrawableTintMode(PorterDuff.Mode)

android:drawableTop

要在文本上方绘制的可绘制对象。

相关方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)

android:editable

如果设置,则指定此 TextView 具有输入法。除非另有说明,否则它将是文本文件。对于 TextView,默认为 false。对于 EditText,默认为 true。

android:editorExtras

对 包含要提供给输入法的附加数据的XML 资源的引用,该 资源对输入法的实现是私有的。这只是在EditorInfo.extras输入法连接时填写该字段。

相关方法:setInputExtras(int)

android:elegantTextHeight

优雅的文本高度,特别是对于不太紧凑的复杂脚本文本。可以是一个布尔值,例如 “true"或"false”。

相关方法:setElegantTextHeight(boolean)

android:ellipsize

如果设置,会导致长于视图宽度的单词被椭圆化而不是在中间断开。您通常还希望设置 scrollHorizo​​ntally 或 singleLine,以便将整个文本也限制在一行中,而不是仍然允许分成多行。

相关方法:setEllipsize(TextUtils.TruncateAt)

android:ems

使TextView正好有这么多em宽。可以是一个整数值,例如" 100"。

相关方法:setEms(int)

android:enabled

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

我这里整理了一份完整的学习思维以及Android开发知识大全PDF,有需要的同学可以自行领取。

资料获取方式:Android完整知识学习体系路线

当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。

izo​​ntally 或 singleLine,以便将整个文本也限制在一行中,而不是仍然允许分成多行。

相关方法:setEllipsize(TextUtils.TruncateAt)

android:ems

使TextView正好有这么多em宽。可以是一个整数值,例如" 100"。

相关方法:setEms(int)

android:enabled

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-p2AxwiCl-1711293440868)]
[外链图片转存中…(img-r8vwejjB-1711293440869)]
[外链图片转存中…(img-r0JxBQnB-1711293440869)]
[外链图片转存中…(img-ysZRCxpZ-1711293440869)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-PKFVHk7A-1711293440870)]

最后

我这里整理了一份完整的学习思维以及Android开发知识大全PDF,有需要的同学可以自行领取。

[外链图片转存中…(img-BPJ6pakJ-1711293440870)]

资料获取方式:Android完整知识学习体系路线

当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值