著作權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/hwe_xc/article/details/50906220
我們知道,TextView控制元件一般是用來顯示文本的,而圖片一般是用ImageView控制元件來顯示。
那TextView能否顯示圖片呢?答案是肯定的!下面列出常見的4種方式。
XML檔案中指定屬性值
這種方式應該是最常用的了,在TextView的左上右下顯示圖片,可用
android:drawableLeft
android:drawableTop
android:drawableRight
android:drawableBottom
比如我們要在TextView的頂部設定圖片,代碼如下:
<TextView
android:id="@+id/textview_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:text="hello_world" />
1
2
3
4
5
6
這種顯示方式圖片跟文本是居中對齊的,此種方式對應的方法是setCompoundDrawablesWithIntrinsicBounds:
mTextView01.setCompoundDrawablesWithIntrinsicBounds(null,
getResources().getDrawable(R.drawable.ic_launcher, null), null, null);
1
2
效果圖:
如果覺得圖片離文字太近,也可以設定他們之間的間距,xml或者代碼中都可以實作:
android:drawablePadding="10dp"
1
或者
mTextView01.setCompoundDrawablePadding(10);
1
通過決議HTML來顯示圖片
這種方式可以顯示專案中的圖片、本地SDCARD和網路的圖片,當然網路的圖片必須先下載到本地然后顯示。
顯示專案中圖片
看代碼
// 第二種方式:顯示專案中的圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/122063.html
標籤:Android
上一篇:c++中字串“v20”轉為20,并用qlcdnumber顯示20,怎么做?
下一篇:請教問題
