0
点赞
收藏
分享

微信扫一扫

Android TextView的属性与应用

梦为马 2022-04-23 阅读 61

android:inputType


android:linksClickable


android:marqueeRepeatLimit


android:ems


android:maxEms


android:minEms


android:maxLength


android:lines


android:maxLines


android:minLines


android:lineSpacingExtra


android:lineSpacingMultiplier


android:numeric


android:password


android:phoneNumber


android:privateImeOptions


android:scrollHorizontally


android:selectAllOnFocus


android:shadowColor


指定文本阴影的颜色,需要与shadowRadius一起使用。

android:shadowDx


android:shadowDy


android:shadowRadius


android:singleLine


android:singleLine=”true”


android:text


android:textAppearance


android:textColor


android:textColorHighlight


android:textColorHint


android:textColorLink


android:textScaleX


android:textSize


android:textStyle


android:typeface


android:height


android:maxHeight


android:minHeight


android:width


android:maxWidth


android:minWidth


TextView的圆角:


这里写图片描述

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

<stroke android:width=“0.5dp”

android:color=“@android:color/black” />

<padding

android:top=“10dp”

android:bottom=“10dp”

android:left=“10dp”

android:right=“10dp” />

<gradient

android:angle=“270”

android:startColor=“#00FFFF”

android:endColor=“#13C7AF” />

光标颜色


修改光标颜色,很容易实现,只需在对应的EditText修改textCursorDrawable属性即可:

<EditText

android:id=“@+id/etCheck”

android:hint=“验证码”

android:textColor=“#0000ff”

android:textCursorDrawable=“@null”

android:inputType=“number”

android:layout_width=“80dp”

android:layout_height=“wrap_content” />

设置为null的话,与textColor颜色一致。

TextView的局部风格化


传统的TextView可以设定样式,比如字体大小,颜色,背景图片,粗体,斜体等。但都是整体风格化,如何让一个TextView中的字体现实不同的效果呢。这个当然不是问题,而且有多种实现方法。

由于只是演示,以功能实现为主,所以不是很美观:

这里写图片描述

下面直接看代码吧。

tv1.setText(“时间过得好快。–Normal”);

tv2.setText(Html

.fromHtml(“HTML:百度粗体蓝色”));搜索

span.setSpan(new ForegroundColorSpan(Color.RED), 8, 17,

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

span.setSpan(new BackgroundColorSpan(Color.YELLOW), 8, 17,

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv3.setText(span);

SpannableString spanStr = new SpannableString(

“字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合”);

// 设置字体(default,default-bold,monospace,serif,sans-serif)

spanStr.setSpan(new TypefaceSpan(“monospace”), 0, 2,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

spanStr.setSpan(new TypefaceSpan(“serif”), 2, 4,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

// 设置字体大小(绝对值,单位:像素)

spanStr.setSpan(new AbsoluteSizeSpan(20), 4, 6,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

spanStr.setSpan(new AbsoluteSizeSpan(20, true), 6, 8,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 第二个参数boolean

// dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。

// 设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍

spanStr.setSpan(new RelativeSizeSpan(0.5f), 8, 10,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 0.5f表示默认字体大小的一半

spanStr.setSpan(new RelativeSizeSpan(2.0f), 10, 12,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 2.0f表示默认字体大小的两倍

// 设置字体前景色

举报

相关推荐

0 条评论