最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android控件View的文字周圍添加圖標(biāo)

 更新時(shí)間:2021年03月31日 09:44:21   作者:飛哥來了  
這篇文章主要為大家詳細(xì)介紹了Android控件View的文字周圍添加圖標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在Android控件View的文字周圍添加圖標(biāo),供大家參考,具體內(nèi)容如下

在控件TextView文字周圍放置圖片(基于TextView的Button也能實(shí)現(xiàn)),減少多布局組合嵌套。

優(yōu)點(diǎn):使用LinearLayout對(duì)ImageViewTextView組合布局固然可行, 但是布局文件會(huì)冗長許多。

以TextView為例:

在XML布局文件中設(shè)置以下5個(gè)屬性:

  • drawableTop: 指定文本上方的圖形。
  • drawableBottom: 指定文本下方的圖形。
  • drawableLeft: 指定文本左邊的圖形。
  • drawableRight: 指定文本右邊的圖形。
  • drawablePadding: 指定圖形與文本的間距。

若在代碼中實(shí)現(xiàn), 則可調(diào)用如下方法。

  • etCompoundDrawables: 設(shè)置文本周圍的圖形。 可分別設(shè)置左邊、 上邊、 右邊、 下邊的圖形。
  • setCompoundDrawablePadding: 設(shè)置圖形與文本的間距。
  • setBounds: 設(shè)置圖形對(duì)象的矩形邊界大小,必須設(shè)置圖片大小,否則不會(huì)顯示圖片。

運(yùn)行效果圖:

示例代碼如下:

public class IconActivity extends AppCompatActivity implements View.OnClickListener {
 private Button btn_icon; // 聲明一個(gè)按鈕對(duì)象
 private Drawable drawable; // 聲明一個(gè)圖形對(duì)象

 @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中獲取圖形對(duì)象
  drawable = getResources().getDrawable(R.drawable.ic_smile);
  // 設(shè)置圖形對(duì)象的矩形邊界大小,注意必須設(shè)置圖片大小,否則不會(huì)顯示圖片
  drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

  // 通過四個(gè)按鈕分別演示:左、上、右、下四個(gè)方向展示圖標(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)擊動(dòng)作,就觸發(fā)監(jiān)聽器的onClick方法
  // 監(jiān)聽到點(diǎn)擊動(dòng)作,就觸發(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決NDK開發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終解決方法(已測(cè))

    解決NDK開發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終解決方法(已測(cè))

    這篇文章主要介紹了解決NDK開發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終方法,需要的朋友可以參考下
    2016-12-12
  • Android仿京東金融首頁頭像效果

    Android仿京東金融首頁頭像效果

    這篇文章主要為大家詳細(xì)介紹了Android 仿京東金融首頁頭像效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 探討:如何在ScrollView中嵌套ListView

    探討:如何在ScrollView中嵌套ListView

    本篇文章是對(duì)如何在ScrollView中嵌套ListView的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Flutter實(shí)現(xiàn)矩形取色器的封裝

    Flutter實(shí)現(xiàn)矩形取色器的封裝

    這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)矩形取色器的封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android Settings 按住電源按鈕的操作方法

    Android Settings 按住電源按鈕的操作方法

    這篇文章主要介紹了Android Settings 按住電源按鈕的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • 解決Android Studio sdk emulator directory is missing問題

    解決Android Studio sdk emulator directory is missing問題

    這篇文章主要介紹了解決Android Studio sdk emulator directory is missing問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android獲取經(jīng)緯度計(jì)算距離介紹

    Android獲取經(jīng)緯度計(jì)算距離介紹

    Android提供LocationManager和Location,可以方便的獲得經(jīng)緯度、海拔等位置。使用LocationManager來獲得位置管理類,從而可以獲得歷史GPS信息以及位置變化的監(jiān)聽注冊(cè);使用Location來獲得具體的位置信息
    2014-01-01
  • Android自定義View之酷炫數(shù)字圓環(huán)

    Android自定義View之酷炫數(shù)字圓環(huán)

    這篇文章主要為大家詳細(xì)介紹了Android自定義View之酷炫數(shù)字圓環(huán),實(shí)現(xiàn)效果很酷,,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 解決Android屏幕四周閃現(xiàn)紅框的問題

    解決Android屏幕四周閃現(xiàn)紅框的問題

    這篇文章主要介紹了解決Android屏幕四周閃現(xiàn)紅框的問題,需要的朋友可以參考下
    2017-06-06
  • Android通過Java sdk的方式接入OpenCv的方法

    Android通過Java sdk的方式接入OpenCv的方法

    這篇文章主要介紹了Android通過Java sdk的方式接入OpenCv的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論

安丘市| 松滋市| 信丰县| 吉隆县| 读书| 金寨县| 九江县| 游戏| 买车| 三都| 萨嘎县| 泰宁县| 中江县| 绵阳市| 兴安盟| 临洮县| 宜章县| 枞阳县| 巴林右旗| 积石山| 田林县| 渑池县| 凉城县| 德州市| 莲花县| 韶关市| 襄汾县| 衡南县| 宁蒗| 泰兴市| 双江| 博乐市| 南康市| 玉溪市| 宁城县| 雷波县| 黔西| 阳高县| 都昌县| 民县| 水富县|