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);