生活随笔
收集整理的這篇文章主要介紹了
TextView图文混排,显示添加的图片,三种常用方法,亲测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖文混排,文字就不說了,主要是顯示圖片的方法
1、TextView使用ImageSpan顯示圖片
[java]?view plaincopy
<span?style="font-size:18px;">ImageSpan?span?=?new?ImageSpan(this,?R.drawable.ic_launcher);?? SpannableString?spanStr?=?new?SpannableString("http://orgcent.com");//不想要這個字符串可以試試空字符串?? spanStr.setSpan(span,?spanStr.length()-1,?spanStr.length(),?Spannable.SPAN_INCLUSIVE_EXCLUSIVE);?? mTVText.setText(spanStr);??
2.使用Html類進行添加,可達到如圖效果,本方法主要是使用html標簽的操作,所有內容可以很豐富,其他用途可自查sdk文檔
完整源碼:TextView圖文混排
[java]?view plaincopy
<span?style="font-size:18px;">public?int?getResourceId(String?name)?{?? ????????try?{?? ?????????????? ????????????Field?field?=?R.drawable.class.getField(name);?? ?????????????? ????????????return?Integer.parseInt(field.get(null).toString());?? ????????}?catch?(Exception?e)?{?? ?????????????? ????????}?? ????????return?0;?? ????}?? ?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ????????TextView?textView?=?(TextView)?this.findViewById(R.id.textview);?? ????????textView.setTextColor(Color.BLACK);?? ????????textView.setBackgroundColor(Color.WHITE);?? ????????textView.setTextSize(20);?? ????????String?html?=?"圖像1<img?src='image1'/>圖像2<img?src='image2'/>圖像3<img?src='image3'/><p>";?? ????????html?+=?"圖像4<a?href='http://www.baidu.com'><img?src='image4'></a>圖像5<img?src='image5'/>";?? ?? ????????CharSequence?charSequence?=?Html.fromHtml(html,?new?ImageGetter()?{?? ?? ????????????@Override?? ????????????public?Drawable?getDrawable(String?source)?{?? ?????????????????? ?????????????????? ????????????????Drawable?drawable?=?getResources().getDrawable(?? ????????????????????????getResourceId(source));?? ?????????????????? ????????????????if?(source.equals("image3"))?{?? ????????????????????drawable.setBounds(0,?0,?drawable.getIntrinsicWidth()?/?2,?? ????????????????????????????drawable.getIntrinsicHeight()?/?2);?? ????????????????}?else?{?? ????????????????????drawable.setBounds(0,?0,?drawable.getIntrinsicWidth(),?? ????????????????????????????drawable.getIntrinsicHeight());?? ????????????????}?? ????????????????return?drawable;?? ????????????}?? ????????},?null);?? ????????textView.setText(charSequence);?? ????????textView.setMovementMethod(LinkMovementMethod.getInstance());</span>??
Html類可以將一些常用的html標簽進行格式化顯示
3、在TextView四周顯示圖片
[java]?view plaincopy
<span?style="font-size:18px;">mTVText.setText("setCompoundDrawables");?? Drawable?d?=?getResources().getDrawable(R.drawable.ic_launcher);?? d.setBounds(0,?0,?50,?50);??? mTVText.setCompoundDrawables(d?,?null,?null,?null);設置圖片顯示的位置(上下左右),此方法的不同之處</span> ?
總結
以上是生活随笔為你收集整理的TextView图文混排,显示添加的图片,三种常用方法,亲测的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。