Android控件View的文字周圍添加圖標(biāo)
在Android控件View的文字周圍添加圖標(biāo),供大家參考,具體內(nèi)容如下
在控件TextView文字周圍放置圖片(基于TextView的Button也能實(shí)現(xiàn)),減少多布局組合嵌套。
優(yōu)點(diǎn):使用LinearLayout對ImageView和TextView組合布局固然可行, 但是布局文件會冗長許多。
以TextView為例:
在XML布局文件中設(shè)置以下5個屬性:
- drawableTop: 指定文本上方的圖形。
- drawableBottom: 指定文本下方的圖形。
- drawableLeft: 指定文本左邊的圖形。
- drawableRight: 指定文本右邊的圖形。
- drawablePadding: 指定圖形與文本的間距。
若在代碼中實(shí)現(xiàn), 則可調(diào)用如下方法。
- etCompoundDrawables: 設(shè)置文本周圍的圖形。 可分別設(shè)置左邊、 上邊、 右邊、 下邊的圖形。
- setCompoundDrawablePadding: 設(shè)置圖形與文本的間距。
- setBounds: 設(shè)置圖形對象的矩形邊界大小,必須設(shè)置圖片大小,否則不會顯示圖片。
:
運(yùn)行效果圖:
示例代碼如下:
public class IconActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_icon; // 聲明一個按鈕對象 private Drawable drawable; // 聲明一個圖形對象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_icon); // 從布局文件中獲取名叫btn_icon的按鈕控件 btn_icon = findViewById(R.id.btn_icon); // 從資源文件ic_launcher.png中獲取圖形對象 drawable = getResources().getDrawable(R.drawable.ic_smile); // 設(shè)置圖形對象的矩形邊界大小,注意必須設(shè)置圖片大小,否則不會顯示圖片 drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); // 通過四個按鈕分別演示:左、上、右、下四個方向展示圖標(biāo)的效果 findViewById(R.id.btn_left).setOnClickListener(this); findViewById(R.id.btn_top).setOnClickListener(this); findViewById(R.id.btn_right).setOnClickListener(this); findViewById(R.id.btn_bottom).setOnClickListener(this); } @Override public void onClick(View v) { // 一旦監(jiān)聽到點(diǎn)擊動作,就觸發(fā)監(jiān)聽器的onClick方法 // 監(jiān)聽到點(diǎn)擊動作,就觸發(fā)監(jiān)聽器的onClick方法 switch (v.getId()) { case R.id.btn_left: // 設(shè)置按鈕控件btn_icon內(nèi)部文字左邊的圖標(biāo) btn_icon.setCompoundDrawables(drawable, null, null, null); break; case R.id.btn_top: // 設(shè)置按鈕控件btn_icon內(nèi)部文字上方的圖標(biāo) btn_icon.setCompoundDrawables(null, drawable, null, null); break; case R.id.btn_right: // 設(shè)置按鈕控件btn_icon內(nèi)部文字右邊的圖標(biāo) btn_icon.setCompoundDrawables(null, null, drawable, null); break; case R.id.btn_bottom: // 設(shè)置按鈕控件btn_icon內(nèi)部文字下方的圖標(biāo) btn_icon.setCompoundDrawables(null, null, null, drawable); break; default: } }
xml中設(shè)置的2行核心代碼
//在控件左側(cè)設(shè)置圖標(biāo) android:drawableLeft="@drawable/ic_smile" //設(shè)置圖標(biāo)與控件文件的間距 android:drawablePadding="10dp"
布局xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="12dp" android:orientation="vertical"> <Button android:id="@+id/btn_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="10dp" android:drawableLeft="@drawable/ic_smile" android:drawablePadding="10dp" android:text="熱烈歡迎" android:textSize="17sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <Button android:id="@+id/btn_left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="圖標(biāo)在左" android:textSize="15sp" /> <Button android:id="@+id/btn_top" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="圖標(biāo)在上" android:textSize="15sp" /> <Button android:id="@+id/btn_right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="圖標(biāo)在右" android:textSize="15sp" /> <Button android:id="@+id/btn_bottom" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="圖標(biāo)在下" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ubantu16.04進(jìn)行Android 8.0源碼編譯的流程
這篇文章主要介紹了Ubantu16.04進(jìn)行Android 8.0源碼編譯的相關(guān)資料,需要的朋友可以參考下2018-02-02android實(shí)現(xiàn)獲取正在運(yùn)行的應(yīng)用程序
android如何獲取正在運(yùn)行的應(yīng)用程序,因?yàn)樵趂ramework中想添加這個功能,所以寫了個appliction來實(shí)現(xiàn)一下獲取正在運(yùn)行的應(yīng)用程序2013-01-01Android開發(fā)Jetpack組件Lifecycle使用篇
這一篇文章來介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學(xué)習(xí)它2022-08-08Android編程之簡單啟動畫面實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程之簡單啟動畫面實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了開機(jī)啟動畫面的制作步驟及布局、Activity跳轉(zhuǎn)、權(quán)限控制等的相關(guān)操作技巧,需要的朋友可以參考下2016-11-11Android采取ContentObserver方式自動獲取驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android采取ContentObserver方式自動獲取驗(yàn)證碼,感興趣的小伙伴們可以參考一下2016-08-08Android音視頻開發(fā)Media FrameWork框架源碼解析
這篇文章主要為大家介紹了Android音視頻開發(fā)Media FrameWork框架源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12android6.0運(yùn)行時權(quán)限完美封裝方法
今天小編就為大家分享一篇android6.0運(yùn)行時權(quán)限完美封裝方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android Studio綁定下拉框數(shù)據(jù)詳解
這篇文章主要為大家詳細(xì)介紹了Android Studio綁定下拉框數(shù)據(jù),Android Studio綁定網(wǎng)絡(luò)JSON數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10