Android中自定义ImageView添加文字说明详解
作者:芯_空 发布时间:2022-10-23 18:39:27
标签:android,自定义imageview,文字说明
前言
大家应该都有所体会,在android开发中,需要展示图片的地方有很多..正常情况下展示一张图片的时候还需要在下面添加一个文字说明..我们也可以用布局ImageView+TextView来实现..最常见的就是底部菜单,或者顶部菜单...图标下面还要添加一个文字说明...重复多次使用ImageView+TextView来实现会感觉有点麻烦..
下面就介绍一个简易的图片+文字的简单控件,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:
上效果图
效果图
下面我们开始撸代码.
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>
配置文件存放位置
下面展示使用方法
来源:http://www.jianshu.com/p/c11fd5a21b87


猜你喜欢
- 栈和队列:都是线性表,都是基于List基础上的实现线性表:数组,链表,字符串,栈,队列元素按照一条“直线&rdq
- JFormDesigner概述jformdesigner是一款功能强大的Swing设计工具,这是全面的程序,可帮助您创建Swing GUI,
- 元注解是负责对其它注解进行说明的注解,自定义注解时可以使用元注解。Java 5 定义了 4 个注解,分别是 @Documented、@Tar
- 上转型对象:子类创建对象 并将这个对象引用赋值给父类的对象。语法格式:Father f=new Son();注意事项:上转型对象是由子类创建
- 1. 初始 Spring Boot1.1 什么是Spring BootSpring 的诞生是为了简化 Java 程序的开发的Spring B
- 先吐槽一下,现在的Bean Searcher操作手册的指引弱的可怜…对我这样的小白及其不友好话不多说直入主题1、首先肯
- 1、未配置之前2、开始配置 2.1 新建一个unauth.html<!DOCTYPE html><html la
- 1.3扫描线种子填充算法1.1和1.2节介绍的两种种子填充算法的优点是非常简单,缺点是使用了递归算法,这不但需要大量栈空间来存储相邻的点,而
- 简介通过 pulsar-flink-connector 读取到 Apache pulsar 中的namespaces、topics的元数据信
- 前言安卓开发中一个很基础的操作就是打开一个 Activity ,另一个很必要的操作就是,打开一个 Activity ,在打开的 Activi
- 本文实例讲述了Android继承ViewGroup实现Scroll滑动效果的方法。分享给大家供大家参考,具体如下:extends ViewG
- ArrayList线程不安全怎么办?有三种解决方法:使用对应的 Vector 类,这个类中的所有方法都加上了 synchronized 关键
- 本文实例讲述了Android编程实现activity dialog透明背景的方法。分享给大家供大家参考,具体如下:首先查一下window&n
- 前言一般数据库的表结构都会有update_time,修改时间,因为这个字段基本与业务没有太大关联,因此开发过程中经常会忘记设置这两个字段的值
- 1.问题引出源码: public static void main(String[] args) { List&l
- 阻塞、无饥饿、无障碍、无锁、无等待几种。阻塞一个线程是阻塞的,那么在其他线程释放资源之前,当前线程无法继续执行。当我们使用synchroni
- Talk is cheap, show me the code!先来看代码:public class TestEval {public st
- Android Service是分为两种:本地服务(Local Service): 同一个apk内被调用远程服务(Remote Servic
- 介绍在 C/C++ 中,程序员负责对象的创建和销毁。通常程序员会忽略无用对象的销毁。由于这种疏忽,在某些时候,为了创建新对象,可能没有足够的
- 微信公众号开发一般是针对企业和组织的,个人一般只能申请订阅号,并且调用的接口有限,下面我们就来简单的描述下接入公众号的步骤:1、首先你需要一