原生TextView能做哪些事。

一、XML

文本基本显示

1、改变文本颜色

自己在color里面定义颜色进行引用

android:textColor="@color/r"
android:textColor="@color/g"
android:textColor="@color/b"

在这里插入图片描述

2、改变文本大小

android:textSize="13sp"
android:textSize="18sp"
android:textSize="10sp"

在这里插入图片描述

3、将文本水平拉伸压缩

android:textScaleX="0.5"
android:textScaleX="1.0"
android:textScaleX="2.0"

在这里插入图片描述

4、给文本添加阴影效果

android:shadowColor="@color/r"
android:shadowRadius="3.0"

android:shadowColor="@color/r"
android:shadowRadius="1.0"
android:shadowDx="20"

android:shadowColor="@color/r"
android:shadowRadius="1.0"
android:shadowDy="20"

在这里插入图片描述

5、改变文本背景颜色

android:background="@color/r"
android:background="@color/g"
android:background="@color/b"

在这里插入图片描述

文本超链接相关

1、超链接文本颜色

需要设定autoLink,常用的是web、phone、email。也可用all检索所有。
另外linksClickable可以设置超链接是否可点击。

android:autoLink="web"
android:textColorLink="@color/r"

android:autoLink="phone"
android:textColorLink="@color/g"

android:autoLink="email"
android:textColorLink="@color/b"

在这里插入图片描述

文本布局位置相关

1、改变文本在组件内显示的位置

相应属性可以用 | 进行组合

android:gravity="start"
android:gravity="center"
android:gravity="end|bottom"

在这里插入图片描述

2、改变文本显示行数

测试发现ellipsize好像对多行的文本无效。

android:singleLine="true"
android:ellipsize="middle"

android:lines="2"

android:maxLines="2"
android:ellipsize="end"

android:minLines="2"

在这里插入图片描述

3、改变文本行距

android:lines="2"

android:lines="2"
android:lineSpacingMultiplier="2"

android:lines="2"
lineSpacingExtra="50dp"

在这里插入图片描述

选中文本

1、改变选中文本左右指示器图标,高亮文本颜色

注意TextView需要添加textIsSelectable=“true”,使文本可选。

android:textColorHighlight="@color/r"
android:textIsSelectable="true"
android:textSelectHandleLeft="@mipmap/ic_launcher"
android:textSelectHandleRight="@mipmap/ic_launcher" 

在这里插入图片描述

其他

1、给文本周边添加视图

可以使用drawablePadding来设置文本内容和周边视图的内边距。

android:drawableStart="@mipmap/ic_launcher"
drawableEnd="@mipmap/ic_launcher"
drawableTop="@mipmap/ic_launcher"
drawableBottom="@mipmap/ic_launcher"

在这里插入图片描述

二、CODE

1、上述XML实现的效果

均可以通过代码实现。另外可实现动态变化的效果。

2、富文本

相关效果来自html,可以查看html相关标签信息,查看其它富文本效果。

        String html2 = "<a href=\"http://www.baidu.com\">超链接</a>    " +
                " <b>黑体</b>" +
                " <big>字体加大</big>" +
                " <blockquote>从两边缩进文本</blockquote>" +
                " <cite>引用(通常是斜体)</cite>" +
                " <br> 换行</br>" +
                " <sub>下标字</sub>" +
                " <strike>加删除线</strike>" +
                " <sup>上标字</sup>" +
                " <u>下划线</u>" +
                " <font color=\"#aabb00\">设置颜色</font>";
        tv_12.setText(Html.fromHtml(html2));
        //图片显示
        Html.ImageGetter imageGetter = new Html.ImageGetter() {
            public Drawable getDrawable(String source) {
                int rId = Integer.parseInt(source);
                Drawable drawable = getResources().getDrawable(rId);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
        };
        String sText = "drawable中的图片:<img src=\"" + R.mipmap.ic_launcher + "\" />";
        tv_13.setText(Html.fromHtml(sText, imageGetter, null));

在这里插入图片描述

3、可滑动文本

文本内容超过组件大小,可上下滑动查看。

tv_11.setMovementMethod(ScrollingMovementMethod.getInstance());

在这里插入图片描述

三、另外想要TextView文本内容自适应组件大小

可使用组件

 <android.support.v7.widget.AppCompatTextView/>

在代码中设置

TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView, 8, 32, 1, TypedValue.COMPLEX_UNIT_SP);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值