android的TextView展示带格式文本、首行缩进、引入自定义字体

来源:互联网 发布:网络客服的职责有哪些 编辑:程序博客网 时间:2024/05/21 02:21

1.TextView可以显示部分带格式的Html内容,包括<img>、<a href="">、<b>、<h1>等等

String ss = "<font color=\"#FF0000\">Hello Android</font><br>"

+"<font color=\"#00FF00\">你好呀</font><br>"

testTextView.setText(Html.fromHtml(ss));

其中带有Img标签的eg:<img:src="+R.drawable.ic_launcher+">,需要给Html.fromHtml()方法额外传递解析图片的ImageGetter对象eg:Html.fromHtml(ss,imgGetter,null);

ImageGetter imgGetter = new ImageGetter(){

public Drawable getDrawable(String source){//遇到<img>标签回调该函数

int id = Integer.parseInt(source);

Drawable d = getResources().getDrawable(id);

d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());

return d;

}

}

2.简单的TextView换行和首行缩进效果

 <TextView
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="公司简介:\n\u3000\u3000有两个空格缩进哦!\n"//\u3000代表一个空格哦
             android:textSize="17sp" />

3.TextView使用自定义字体

将字体文件一般为.ttf后缀,拷贝到asset/fonts目录下

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/我的字体.TTF");

myTextView.setTypeface(tf);

4.通过xml布局文件生成相应的View

LayoutInflater inflater = getLayoutInflater();
 View view = inflater.inflate(R.layout.listview_childitem, null);//生成View
Button check = (Button) view.findViewById(R.id.listview_check);//使用布局文件中的子View

注:以上代码是纯手打,仅是思路


0 0
原创粉丝点击