Android中自定义ImageView添加文字说明详解

作者:芯_空 时间:2022-10-23 18:39:27 

前言

大家应该都有所体会,在android开发中,需要展示图片的地方有很多..正常情况下展示一张图片的时候还需要在下面添加一个文字说明..我们也可以用布局ImageView+TextView来实现..最常见的就是底部菜单,或者顶部菜单...图标下面还要添加一个文字说明...重复多次使用ImageView+TextView来实现会感觉有点麻烦..

下面就介绍一个简易的图片+文字的简单控件,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:

上效果图

Android中自定义ImageView添加文字说明详解
效果图

下面我们开始撸代码.

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);//设置垂直排序
 this.setGravity(Gravity.CENTER);//设置居中
 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);//获取属性名称
  //根据属性获取资源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();
}

/**
 * 初始化状态
 */
private void init() {
 this.setText(textId);
 mTextView.setGravity(Gravity.CENTER);//字体居中
 this.setTextColor(textColorId);
 this.setImgResource(imageId);
 addView(mImageView);//将图片控件加入到布局中
 addView(mTextView);//将文字控件加入到布局中
}

/**
 * 设置显示的图片
 *
 * @param resourceID 图片ID
 */
private void setImgResource(int resourceID) {
 if (resourceID == 0) {
  this.mImageView.setImageResource(0);
 } else {
  this.mImageView.setImageResource(resourceID);
 }
}

/**
 * 设置显示的文字
 *
 * @param text
 */
public void setText(int text) {
 this.mTextView.setText(text);
}

/**
 * 设置字体颜色(默认为黑色)
 *
 * @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>

Android中自定义ImageView添加文字说明详解

配置文件存放位置

下面展示使用方法

Android中自定义ImageView添加文字说明详解
实际使用

来源:http://www.jianshu.com/p/c11fd5a21b87

标签:android,自定义imageview,文字说明
0
投稿

猜你喜欢

  • Spring运行时动态注册bean的方法

    2023-11-25 04:16:58
  • Android Studio gradle 编译提示‘default not found’ 解决办法

    2023-07-19 09:23:06
  • Intellij IDEA中启动多个微服务(开启Run Dashboard管理)

    2022-01-11 02:25:00
  • SpringBoot 静态资源导入及首页设置问题

    2023-11-26 22:45:07
  • SpringBoot整合RocketMQ的详细过程

    2023-07-10 05:07:00
  • c#哈希算法的实现方法及思路

    2023-10-22 01:48:08
  • Java原生HttpClient的使用详解

    2022-06-04 16:29:49
  • 对int array进行排序的实例讲解

    2021-12-09 06:51:15
  • C# Winform实现圆角无锯齿按钮

    2022-08-30 01:11:56
  • 一文详解Spring的Enablexxx注解使用实例

    2023-09-03 08:43:18
  • 微信公众号支付(一)如何获取用户openId

    2022-07-29 04:13:57
  • Spring Boot 配置和使用多线程池的实现

    2022-09-04 19:53:02
  • java提取json中某个数组的所有值方法

    2022-04-25 16:02:04
  • 理解Java中的静态绑定和动态绑定

    2022-05-28 04:20:06
  • C语言深入浅出解析二叉树

    2022-05-12 11:28:21
  • Jenkins+Git+Maven自动化部署配置详解

    2022-05-20 22:24:43
  • 源码解析JDK 1.8 中的 Map.merge()

    2023-11-16 23:49:25
  • Android编程基于Contacts读取联系人的方法(附demo源码)

    2023-11-22 06:05:39
  • Android 按后退键退出Android程序的实现方法

    2023-06-16 20:08:02
  • 全面理解java中的异常处理机制

    2023-10-26 04:08:20
  • asp之家 软件编程 m.aspxhome.com