文字竖直显示
设置TextView的android:ems="1"
身份证号码隐藏出生年月日期
//隐藏身份证号码出生年月
public static final String ID_CARD_HIDE = "(\\d{6})\\d{8}(\\w{4})";
String idCardHide = idCard.replaceAll(ID_CARD_HIDE, "$1*****$2");
textView.setText(idCardHide);
设置图标
1、布局里设置
设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom属性
- android:drawablePadding:文字和图片间距
- android:alpha:可设置透明度(0~1)
<TextView
android:id="@id/common_timeline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="5dp"
android:drawableTop="@drawable/ico_wx_time_line3"
android:gravity="center"
android:text="@string/share_hint_2"
android:textSize="13sp"
android:textColor="@android:color/white"/>
image.png
2、代码中设置
- 可调整图标大小和位置
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.ico_go_list);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textView.setCompoundDrawables(null, null, drawable, null);
- 直接显示图标:按照原有图片的比例大小显示
//设置间距
textView.setCompoundDrawablePadding(getApp().getPadding(5));
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0);
-
方法区别
setCompoundDrawables(left, top, right, bottom)
drawable的宽高是按drawable.setBound()设置的
使用之前必须使用Drawable.setBounds设置Drawable的长宽
setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
drawable的宽高是按drawable固定的宽高
可通过getIntrinsicWidth()与getIntrinsicHeight()获得
部分字体样式
通过SpannableStringBuilder对象我们可以设置textview的各种样式(EditText同样适用)
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端 (start, end)
- Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包start,包含end (start, end]
- Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含start,不包含end [start, end)
- Spanned.SPAN_INCLUSIVE_INCLUSIVE --- 包含两端 [start, end]
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(textView.getText());
//字体
spannableStringBuilder.setSpan(new TypefaceSpan("PingFangSC-Semibold"),0,3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//字体颜色
spannableStringBuilder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.yellow_color2)),0,14,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//字体背景色
spannableStringBuilder.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//字号
spannableStringBuilder.setSpan(new AbsoluteSizeSpan(18,true),0,3,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//粗体、斜体
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//删除线
spannableStringBuilder.setSpan(new StrikethroughSpan(), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//下划线
spannableStringBuilder.setSpan(new UnderlineSpan(), 1, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//字体换图片
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
drawable .setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
spanString.setSpan(new ImageSpan(d, ImageSpan.ALIGN_BASELINE), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//最后--设置属性
textView.setText(spannableStringBuilder);
-
SpannableStringBuilder也可改为SpannableString
SpannableString ss = new SpannableString(textView.getText()); -
方法区别
SpannableStringBuilder&&SpannableString
类似StringBuilder和String,SpannableStringBuilder比SpannableString多了个append()方法来拼接多个String,其他用法差不多。
部分文字可点击
直接上代码
- 新建TextClickableSpan继承ClickableSpan(可直接复制使用)
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
/**
* 文字链接样式-可自定义颜色(默认无下划线)
*/
public class TextClickableSpan extends ClickableSpan {
private OnClickSpanListener onClickSpanListener;
private int color;
@Override
public void onClick(View view) {
if (onClickSpanListener != null)
onClickSpanListener.onClickTextSpan(view);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(color);
ds.setUnderlineText(false);
}
public void setOnClickSpanListener(OnClickSpanListener onClickSpanListener) {
this.onClickSpanListener = onClickSpanListener;
}
public void setColor(int color) {
this.color = color;
}
public interface OnClickSpanListener {
void onClickTextSpan(View view);
}
}
- 在要使用的TextView中加入以下代码
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(textView.getText());
//设置部分文字可点击
TextClickableSpan textClickableSpan = new TextClickableSpan();
textClickableSpan.setOnClickSpanListener(this);
textClickableSpan.setColor(getResources().getColor(R.color.blue_0076ff));
stringBuilder.setSpan(textClickableSpan, 7, textView.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//这个记得设置,不然点击事件不生效
textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setHighlightColor(Color.TRANSPARENT);
textView.setText(stringBuilder);
重写方法:监听文字的点击事件
@Override//在这里写文字点击监听事件
public void onClickTextSpan(View view) {
//todo
}
网友评论