欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中自定義ImageView添加文字說明詳解

 更新時間:2017年08月23日 10:19:17   作者:芯_空  
Android中的ImageView只能顯示矩形的圖片,為了用戶體驗更多,下面這篇文章主要給大家介紹了關(guān)于Android中自定義ImageView實現(xiàn)添加文字說明的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

大家應(yīng)該都有所體會,在android開發(fā)中,需要展示圖片的地方有很多..正常情況下展示一張圖片的時候還需要在下面添加一個文字說明..我們也可以用布局ImageView+TextView來實現(xiàn)..最常見的就是底部菜單,或者頂部菜單...圖標下面還要添加一個文字說明...重復(fù)多次使用ImageView+TextView來實現(xiàn)會感覺有點麻煩..

下面就介紹一個簡易的圖片+文字的簡單控件,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹:

上效果圖


效果圖

下面我們開始擼代碼.

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è)置字體顏色(默認為黑色)
  *
  * @param color
  */
 private void setTextColor(int color) {
  if (color == 0) {
   this.mTextView.setTextColor(Color.BLACK);
  } else {
   this.mTextView.setTextColor(getResources().getColor(color));
  }
 }

}

簡單解釋下..實際上就是在LinearLayout布局中添加ImageView和TextView

這個View也比較簡單,代碼中也有部分簡易的說明.

下面可能還需要一個屬性文件

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>

配置文件存放位置

下面展示使用方法


實際使用

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

  • Android中fragment嵌套fragment問題解決方法

    Android中fragment嵌套fragment問題解決方法

    這篇文章主要介紹了Android中fragment嵌套fragment問題解決方法,本文給出兩個解決方法,需要的朋友可以參考下
    2015-06-06
  • Android源碼解析之截屏事件流程

    Android源碼解析之截屏事件流程

    這篇文章我們主要講一下Android系統(tǒng)中的截屏事件處理流程。用過android系統(tǒng)手機的同學應(yīng)該都知道,一般的android手機按下音量減少鍵和電源按鍵就會觸發(fā)截屏事件.
    2018-05-05
  • Android Activity 與Service進行數(shù)據(jù)交互詳解

    Android Activity 與Service進行數(shù)據(jù)交互詳解

    這篇文章主要介紹了Android Activity 與Service進行數(shù)據(jù)交互的相關(guān)資料,在開發(fā)Android App的時候經(jīng)常會使用這樣的功能,需要的朋友可以參考下
    2016-10-10
  • 為Android添加一門新語言的解決辦法

    為Android添加一門新語言的解決辦法

    本篇文章是對為Android添加一門新語言的解決方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android讀取XML文件中的數(shù)據(jù)

    Android讀取XML文件中的數(shù)據(jù)

    這篇文章主要為大家詳細介紹了Android讀取XML文件中的數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • flutter實現(xiàn)發(fā)送驗證碼功能

    flutter實現(xiàn)發(fā)送驗證碼功能

    這篇文章主要為大家詳細介紹了flutter發(fā)送驗證碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android入門之ProgressBar的使用教程

    Android入門之ProgressBar的使用教程

    Android里的ProgressBar默認為一個不斷轉(zhuǎn)圈的圓,它也可以自定義。這篇文章主要通過示例和大家介紹下ProgressBar的使用,感興趣的可以了解一下
    2022-11-11
  • Android編程實現(xiàn)圖片平鋪的方法分析

    Android編程實現(xiàn)圖片平鋪的方法分析

    這篇文章主要介紹了Android編程實現(xiàn)圖片平鋪的方法,結(jié)合具體實例形式總結(jié)分析了Android實現(xiàn)圖片平鋪效果的三種常用操作技巧,需要的朋友可以參考下
    2017-06-06
  • Android中簡單調(diào)用圖片、視頻、音頻、錄音和拍照的方法

    Android中簡單調(diào)用圖片、視頻、音頻、錄音和拍照的方法

    這篇文章主要介紹了Android中簡單調(diào)用圖片、視頻、音頻、錄音和拍照的方法,涉及Android多媒體操作的常用技巧,需要的朋友可以參考下
    2016-08-08
  • 最新評論