android TextView相关属性及文本处理
来源:互联网 发布:惠普q1910更新软件 编辑:程序博客网 时间:2024/05/18 23:53
textview 设置图片两种方式:
TextView textView=null;
1, int flagResId = getResources().getIdentifier("icon", "drawable", getPackageName());
textView.setCompoundDrawablesWithIntrinsicBounds(flagResId, 0, 0, 0);
2, Drawable drawable = mContext.getResources().getDrawable(R.drawable.shop_arrow_down);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textView.setCompoundDrawables(null, null, drawable, null);
TextView控件的背景透明度和字体透明度
方式1,
tv.setBackgroundColor(Color.argb(255, 0, 255, 0)); //背景透明度
tv.setTextColor(Color.argb(255, 0, 255, 0)); //文字透明度
方式2,
tv.setTextColor(0xffff00ff);
0xffff00ff是int类型的数据,分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示颜色,注意:这里ffff00ff必须是8个的颜色表示
原理,把透明度转换为色值。就是根据透明度转换为色值,放在前两位
one textView different text style
String s= "Hello Everyone";
SpannableString ss1= new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1);
//设置字体大小(绝对值,单位:像素)
// msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// msp.setSpan(new AbsoluteSizeSpan(20,true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。
// //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍
// msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //0.5f表示默认字体大小的一半
// msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体大小的两倍
// msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置前景色为洋红色
// msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置背景色为青色
textView.setText(Html.fromHtml("<b>myLogin</b> <i>logout</i>"));SpannableString styledString = new SpannableString("myLogin logout");
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 8, 14, 0);
tv.setText(styledString);
计算字体宽度:
public static float GetTextWidth(String text, float Size) {
TextPaint FontPaint = new TextPaint();
FontPaint.setTextSize(Size);
return FontPaint.measureText(text);
}
// 计算文本,行数,高度
FontMetrics fm = mTextView.getPaint().getFontMetrics();
mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得每行高度
mPageLineNum = (int) (mTextHeight / mFontHeight);// 获得行数
//TextView 分页设置相关
getLineBounds(int line, Rect bounds) // 得到指定行的边界
只要从第一行开始一行一行往下看, 直到找到超出边界的那一行, 就能知道这个 TextView 能显示多少行了.
或者用 getHeight() / getLineHeight() 也能获取 TextView 的最大显示行数
getLineForVertical(int vertical) // 根据纵坐标得到对应的行号
getLineEnd(int line) // 返回指定行中最后一个字在整个字符串中的位置
一个TextView设置多种颜色文字
Html.fromHtml(String.format(formate, getColorText(params, color)));
//TextView设置完setMaxLines后,通过TextView.getHeight方法获取的是当前行数的高度,而非文字完全显示的高度
//实际获取高度方法
//自定义View绘制文本时,格式化文本内容StaticLayout实现:
public void onDraw(Canvas canvas){
super.onDraw(canvas);
TextPaint tp = new TextPaint();
tp.setColor(Color.BLUE);
tp.setStyle(Style.FILL);
tp.setTextSize(50);
String message = "paint,draw paint指用颜色画,如油画颜料、水彩或者水墨画,而draw 通常指用铅笔、钢笔或者粉笔画,后者一般并不涂上颜料。两动词的相应名词分别为p";
StaticLayout myStaticLayout = new StaticLayout(message, tp, canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
myStaticLayout.draw(canvas);
canvas.restore();
}
自定义view绘制文本计算文本宽度高度:
文本分割处理
TextView字体间距
1、Android:lineSpacingExtra
设置行间距,如”3dp”。
2、android:lineSpacingMultiplier
设置行间距的倍数,如”1.2″。
设置texview 垂直滚动条
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollbars="vertical"
android:singleLine="false"
设置textview 文字水平自动滚动(跑马灯效果)
<com.example.playpic.MyTextView
android:id="@+id/myTv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
/>
布局文件需添加属性:android:addStatesFromChildren="true"
修改的textview
setContentView(R.layout.scrollview1);
MyTextView tv=(MyTextView)findViewById(R.id.myTv);
tv.setText(str);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());
android 动态设置TextView值,例:金额增加
Textview 设置maxLines>1时,超出部分内容没...省略号解决方法
解决方案:1,自定义实现,2,通过post(Runable r)即在Textview设置文字后显示完计算处理
使用:
tv_attr.setText(txtAttr);
OmitTextViewDeal attrTvDeal=new OmitTextViewDeal();
attrTvDeal.tv=tv_attr;
attrTvDeal.txt=txtAttr;
holder.tv_attr.post(attrTvDeal);
- android TextView相关属性及文本处理
- android TextView相关属性及文本处理
- Android TextView相关属性
- android文本组件textView属性autolink设置为web,显示网址的处理方法
- android TextView 相关属性设置(…
- Android之TextView组件相关属性详解
- TextView文本显示相关
- css样式之文本相关属性及边框相关属性
- 【Android】03、TextView控件及属性
- TextView 相关属性
- TextView相关属性
- TextView相关属性
- TextView 的 XML 属性及相关方法的说明
- Android 文本动态处理方法,TextView动态赋值。
- Android Dialog自定义处理类textView文本不显示
- TextView省略&相关处理
- Android textAppearance的属性设置及TextView属性详解
- Android textAppearance的属性设置及TextView属性详解
- 青蛙跳台阶/变态跳台阶问题
- pthread_create源码分析
- 20170822-BIN
- hdu 2063 过山车【二分图】
- bzoj 1138: [POI2009]Baj 最短回文路 bfs
- android TextView相关属性及文本处理
- 23中设计模式之策略模式
- 8-22 DAIRY
- Go 程序的性能优化及 pprof 的使用
- MySQL 数据库常用命令小结
- MemoryPool —— 简单高效的内存池 allocator 实现
- Idea使用总结
- vim 显示行号 自动缩进 语法高亮等设置
- python学习——类中为什么要定义__init__()方法