溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中怎么通過(guò)自定義ImageView添加文字說(shuō)明

發(fā)布時(shí)間:2021-06-26 17:09:24 來(lái)源:億速云 閱讀:127 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章為大家展示了Android中怎么通過(guò)自定義ImageView添加文字說(shuō)明,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

MyImageTextViewNew.java

public class MyImageTextViewNew extends LinearLayout {

 private ImageView mImageView = null;
 private TextView mTextView = null;
 private int imageId;
 private int textId, textColorId;

 public MyImageTextViewNew(Context context) {
  this(context, null);
 }

 public MyImageTextViewNew(Context context, @Nullable AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public MyImageTextViewNew(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  this.setOrientation(LinearLayout.VERTICAL);//設(shè)置垂直排序
  this.setGravity(Gravity.CENTER);//設(shè)置居中
  if (mImageView == null) {
   mImageView = new ImageView(context);
  }
  if (mTextView == null) {
   mTextView = new TextView(context);
  }
  if (attrs == null)
   return;
  int count = attrs.getAttributeCount();
  for (int i = 0; i < count; i++) {
   String attrName = attrs.getAttributeName(i);//獲取屬性名稱
   //根據(jù)屬性獲取資源ID
   switch (attrName) {
    //顯示的圖片
    case "image":
     imageId = attrs.getAttributeResourceValue(i, 0);
     break;
    //顯示的文字
    case "text":
     textId = attrs.getAttributeResourceValue(i, 0);
     break;
    //顯示的文字的顏色
    case "textColor":
     textColorId = attrs.getAttributeResourceValue(i, 0);
     break;
   }
  }
  init();
 }

 /**
  * 初始化狀態(tài)
  */
 private void init() {
  this.setText(textId);
  mTextView.setGravity(Gravity.CENTER);//字體居中
  this.setTextColor(textColorId);
  this.setImgResource(imageId);
  addView(mImageView);//將圖片控件加入到布局中
  addView(mTextView);//將文字控件加入到布局中
 }

 /**
  * 設(shè)置顯示的圖片
  *
  * @param resourceID 圖片ID
  */
 private void setImgResource(int resourceID) {
  if (resourceID == 0) {
   this.mImageView.setImageResource(0);
  } else {
   this.mImageView.setImageResource(resourceID);
  }
 }

 /**
  * 設(shè)置顯示的文字
  *
  * @param text
  */
 public void setText(int text) {
  this.mTextView.setText(text);
 }

 /**
  * 設(shè)置字體顏色(默認(rèn)為黑色)
  *
  * @param color
  */
 private void setTextColor(int color) {
  if (color == 0) {
   this.mTextView.setTextColor(Color.BLACK);
  } else {
   this.mTextView.setTextColor(getResources().getColor(color));
  }
 }

}

簡(jiǎn)單解釋下..實(shí)際上就是在LinearLayout布局中添加ImageView和TextView

這個(gè)View也比較簡(jiǎn)單,代碼中也有部分簡(jiǎn)易的說(shuō)明.

下面可能還需要一個(gè)屬性文件

imageText.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <declare-styleable name="imageText">
  <attr name="image" format="integer" />
  <attr name="text" format="integer" />
  <attr name="textColor" format="integer" />
 </declare-styleable>
 
</resources>

Android中怎么通過(guò)自定義ImageView添加文字說(shuō)明

配置文件存放位置

下面展示使用方法

Android中怎么通過(guò)自定義ImageView添加文字說(shuō)明

上述內(nèi)容就是Android中怎么通過(guò)自定義ImageView添加文字說(shuō)明,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI