标题
重点内容
textSize,默认大小是15sp
标题
图文混排
TextView走马灯效果
1,适配性较高:
基层TextView覆写isFocused()方法,return true;。使用自定义的View配置如下属性:
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:singleLine="true"
//在代码中调用setSelected();
MarqueTextView tv=(MarqueTextView)findViewById(R.id.my_text_view);
tv.setSelected(true);
2
android:layout_width="50px" android:layout_height="wrap_content"
android:singleLine="true"
android:focusable="true"
android:text="跑马灯效果 "
android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
加载Html
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.textView1);
String html="<html><head><title>TextView使用HTML</title></head><body><p><strong>强调</strong></p><p><em>斜体</em></p>"
+"<p><a href=\"http://www.dreamdu.com/xhtml/\">超链接HTML入门</a>学习HTML!</p><p><font color=\"#aabb00\">颜色1"
+"</p><p><font color=\"#00bbaa\">颜色2</p><h1>标题1</h1><h3>标题2</h3><h6>标题3</h6><p>大于>小于<</p><p>" +
"下面是网络图片</p><img src=\"http://avatar.csdn.net/0/3/8/2_zhang957411207.jpg\"/></body></html>";
tv.setMovementMethod(ScrollingMovementMethod.getInstance());//滚动
tv.setText(Html.fromHtml(html));
}
TextView使用HTML处理字体样式、显示图片等
TextView加载html内容并加载图片
控件滑动
xml文件中:
android:scrollbars="vertical" //也可使用其他类型
//自定义滚动条。
android:scrollbarThumbVertical="@drawable/ic_launcher" //滑块的图片
android:scrollbarTrackVertical="@drawable/ic_launcher" //滑道的图片
TextView textview = (TextView) findViewById(R.id.text);
/** *
* 只有调用了该方法,TextView才能不依赖于ScrollView而实现滚动的效果。
* 要在XML中设置TextView的textcolor,否则,当TextView被触摸时,会灰掉。
*/
textview.setMovementMethod(ScrollingMovementMethod.getInstance());
android:scrollbarStyle属性及滚动条和分割线覆盖问题
maxLines,lines,singleLine区别
maxLines:
Makes the TextView be at most this many lines tall(高). When used on an editable text, the inputType attribute’s value must be combined with the textMultiLine flag for the maxLines attribute to apply.
lines:
Makes the TextView be exactly(准确) this many lines tall.
singleLine:
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key. The default value is false (multi-line wrapped text mode) for non-editable text, but if you specify any value for inputType, the default is true (single-line input field mode).
小技巧
tools:text="用户名"
引入tools命名空间后,使用如上属性。可以在Design页面预览时显示字符,在运行时不显示。
排版
textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距
// 调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
字间距
textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型。查看源代码,默认textView 此属性是使用的是:
android.internal.R.styleable.TextView_textScaleX
setTextScaleX(a.getFloat(attr, 1.0f));
行间距
Android系统中TextView默认显示中文时会比较紧凑,不是很美观。为了让每行保持一定的行间距,可以设置属性android:lineSpacingExtra或android:lineSpacingMultiplier。
关于Android下TextView中文换行问题,可查看Android自定义view-文本自动换行。
1、android:lineSpacingExtra
设置行间距,如”3dp”。
2、android:lineSpacingMultiplier
设置行间距的倍数,如”1.2″。
富文本
TextView 局部文字变色,富文本支持:SpannableStringBuilder
方法
1: 设置文本是否包含顶部和底部的额外空白
android:includeFontPadding="false"
2: textview中可以显示多行,但是行数是有限制的,超出显示singleline的效果
android:ellipsize="end"
android:maxLines="2"
android:lines="2" //效果同上,区别但验证。
3: 文字的雕刻效果
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="28px"
android:textColor="#ffffff" android:gravity="center_vertical|center_horizontal"
android:background="@drawable/title_bg"
android:text="雕刻效果的文字"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="-2"
android:shadowRadius="0.1"
API
android:
android:ellipsize=“start|end|middle|marquee” //省略号在开头,中间,结尾,跑马灯
标题
- TextView的drawLeft、drawRight
- TextView 高级教程
- 关于EditText中的TextWatcher监听
- TextView添加点击事件
- 使用代码为textview设置drawableLeft
- 基线(Baseline)的含义以及在Android中的使用
- 关于在TextView中设置DrawableLeft的问题
- Android实现加载富文本以及SpannableString、SpannableStringBuilder实现部分文字可以点击,更换颜色
- 限制文字长度
- 字体大小自适应的TextView
- 花式TextView,探索 Android 中的 Span
- 不到100行代码实现左右对齐TextView
- 聊一聊Android中的字体适配