Android记录2--TextView相关小知识

限制textview只有两行且多余行用省略号代替

1
2
3
4
5
6
7
8
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hospital_abstract_content"
android:lines="2"
android:maxLines="2"
android:ellipsize="end"
/>

android:maxLines=”2” 最大行数限制,如果超过两行也不会再显示了
android:ellipsize=”end” 超过规定长度则使用末尾省略号
android:lines=”2” 显示两行,即使text只有一行也会占位两行的高度

去除textview的默认padding

1
android:includeFontPadding="false"

文字加空格

空格:  
窄空格:  

首行缩进两字符:
•在string资源文件中,在文字的前面加入”\u3000\u3000”即可实现首行缩进

TextView修改部分文字颜色

方法一:

1
2
3
4
SpannableStringBuilder builder1 = new SpannableStringBuilder(tvMallDaily.getText().toString());
ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
builder1.setSpan(redSpan, 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvMallDaily.setText(builder1);

方法二:

1
2
<string name="hh_no_order"><![CDATA[sorry,没有任何订单,<font color="#fc2a56">前往买买买</font>]]></string>
mTvTip.setText(Html.fromHtml(mTips))