国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android中自定義ImageView添加文字說(shuō)明詳解

2019-12-12 02:12:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

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

下面就介紹一個(gè)簡(jiǎn)易的圖片+文字的簡(jiǎn)單控件,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(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>

配置文件存放位置

下面展示使用方法


實(shí)際使用

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 柏乡县| 江达县| 孟村| 光山县| 乡宁县| 鸡东县| 新乐市| 阿坝县| 城口县| 平塘县| 曲靖市| 措美县| 象山县| 云龙县| 井研县| 紫阳县| 嘉善县| 青州市| 淮阳县| 太仓市| 惠州市| 栾川县| 信阳市| 荥阳市| 长阳| 陈巴尔虎旗| 疏勒县| 襄樊市| 迁安市| 都昌县| 富裕县| 九龙坡区| 永泰县| 南康市| 三门峡市| 千阳县| 会昌县| 丘北县| 昔阳县| 库尔勒市| 灵川县|