首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android TextView 末尾動態添加圖片:drawableRight XML 屬性最簡單

     編輯:程式設計疑難解答

XML 靜態配置

  

動態控制顯示/隱藏

TextView tv = findViewById(R.id.tv);
Drawable icon = ContextCompat.getDrawable(this, R.drawable.ic_icon);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
// 顯示
tv.setCompoundDrawables(null, null, icon, null);
// 隱藏
tv.setCompoundDrawables(null, null, null, null);

優勢

  • 無需額外 ImageView,佈局簡潔
  • 自動參與文字換行、對齊計算
  • drawablePadding 精確控制間距
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved