Android自定义实现图片加文字功能
作者:lqh 发布时间:2022-08-03 22:05:49
标签:Android,自定义,图片文字
Android自定义实现图片加文字功能
分四步来写:
1,组合控件的xml;
2,自定义组合控件的属性;
3,自定义继承组合布局的class类,实现带两参数的构造器;
4,在xml中展示组合控件。
具体实现过程:
一、组合控件的xml
我接触的有两种方式,一种是普通的Activity的xml;一种是父节点为merge的xml。我项目中用的是第一种,但个人感觉第二种好,因为第一种多了相对或者绝对布局层。
我写的 custom_pictext.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/custom_pic_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/a" />
<TextView
android:id="@+id/custom_date_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/custom_pic_iv"
android:layout_marginBottom="5dp"
android:layout_marginLeft="8dp"
android:text="2017" />
<TextView
android:id="@+id/custom_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/custom_pic_iv"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:text="题目" />
</RelativeLayout>
这里展示一个merge的例子,有时间,大家可以自己体会下。
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</merge>
这两个xml,都是写在layout中的。
二、自定义组合控件的属性
这步是我们自定义的重要部分之一,自定义组件的私有特性全显示在这。
首先在values中创建attrs.xml
然后定义属性,如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<declare-styleable name="CustomPicText">
<attr name="pic_backgroud" format="reference"/>
<attr name="pic_backgroud_width" format="dimension"/>
<attr name="pic_backgroud_height" format="dimension"/>
<attr name="pic_text" format="string"/>
<attr name="pic_text_color" format="color"/>
<attr name="pic_text_size" format="integer"/>
<attr name="pic_date" format="string"/>
<attr name="pic_date_color" format="color"/>
<attr name="pic_date_size" format="integer"/>
</declare-styleable>
</resources>
这里有几点需要注意的,第一:属性名为name,第二:属性单位为fromat。这单位包含的值可以查看这里。
三、自定义继承组合布局的class类,实现带两参数的构造器
我实现的CustomPicText.Java
/**
* Created by Hman on 2017/5/4.
* 为了测试自定义组合控件
*/
public class CustomPicText extends RelativeLayout {
private ImageView customPicIv;
private TextView customDateTv;
private TextView customTextTv;
public CustomPicText(Context context, AttributeSet attrs) {
super(context, attrs);
// 加载layout
View view = LayoutInflater.from(context).inflate(R.layout.custom_pictext,this);
customPicIv = (ImageView) view.findViewById(R.id.custom_pic_iv);
customDateTv = (TextView) view.findViewById(R.id.custom_date_tv);
customTextTv = (TextView) view.findViewById(R.id.custom_text_tv);
// 加载自定义属性配置
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CustomPicText);
// 为自定义属性添加特性
if (typedArray != null) {
// 为图片添加特性
int picBackgroud = typedArray.getResourceId(R.styleable.CustomPicText_pic_backgroud, 0);
float picWidth = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_width,25);
float picHeight = typedArray.getDimension(R.styleable.CustomPicText_pic_backgroud_height,25);
customPicIv.setBackgroundResource(picBackgroud);
// customPicIv.setMinimumWidth(picWidth);
// 为标题设置属性
String picText = typedArray.getString(R.styleable.CustomPicText_pic_text);
int picTextColor = typedArray.getColor(R.styleable.CustomPicText_pic_text_color,16);
int picTextSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 16);
customTextTv.setText(picText);
customTextTv.setTextColor(picTextColor);
customTextTv.setTextSize(picTextSize);
// 为日期设置属性
String picDate = typedArray.getString(R.styleable.CustomPicText_pic_date);
int picDateColor = typedArray.getColor(R.styleable.CustomPicText_pic_date_color, 0);
int picDateSize = typedArray.getResourceId(R.styleable.CustomPicText_pic_date_size, 12);
customDateTv.setText(picDate);
customDateTv.setTextColor(picDateColor);
customDateTv.setTextSize(picDateSize);
typedArray.recycle();
}
}
}
在这里,我们也可以给控件添加一些 * ,大家自己去加上;这里值得注意的是一个加载配置的类TypeArray
4,在xml中展示组合控件
这个随便写到一个xml中去就行
代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:hman="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.eastsun.widget.CustomPicText
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
hman:pic_backgroud="@mipmap/b"
hman:pic_date="2017/5/6"
hman:pic_date_color="@color/white"
hman:pic_text="第一张图片"
hman:pic_text_color="@color/red"
hman:pic_text_size="18"></com.eastsun.widget.CustomPicText>
</LinearLayout>
这里有一点别忘记,添加xmlns:hman=”http://schemas.Android.com/apk/res-auto”
总结
程序基本上到这就结束了。
看下效果截图
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/mo_feng_/article/details/71191258


猜你喜欢
- 一、RESTful 简介REST 是一种软件架构风格。REST:Representational State Transfer,表现层资源状
- 前言最近在学习安卓开发的时候遇到了一个问题,使用Android Studio在为Button设置背景颜色的时候发现设置好后却在运行 * 上失
- 一、系统介绍本系统实现了用户登录,实现了对学生成绩的增删改查,实现了用户修改密码功能,采用MD5加密算法,数据库使用Mysql8.0.13,
- 本文实例讲述了C# DataTable中Compute方法用法。分享给大家供大家参考,具体如下:Compute函数的参数就两个:Expres
- java 制作验证码并进行验证实例详解在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码
- 一、概述有序数组中常常用到二分查找,能提高查找的速度。今天,我们用顺序查找和二分查找实现数组的增删改查。二、有序数组的优缺点优点:查找速度比
- 一、javaBeanjavaBean:一种类的规格编写规范javaBean在MVC设计模型中是model,又称模型层,在一般的程序中,我们称
- 众所周知,android里面我们很熟悉的一个功能,侧滑菜单效果在以前我们大部分都是用的slidingmenu这个开源框架,自从谷歌官方新出的
- 一、示例搭建步骤先给出本文示例代码:WpfWithCefSharpDemo。1. 创建项目创建一个WPF项目,比如命名为&ldquo
- 在需要线程同步的时候如何选择合适的线程锁?例:选择可以存入到常量池当中的对象,String对象等public class SyncTest{
- 目录带装饰效果的 ContainerRow 行布局和 Column列布局ListView列表组件GridView网格组件代码实现结语:左侧是
- 这篇文章主要介绍了Java日期与时间类原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 免责声明:本教程所有资源均来源于网络;仅用于学习交流,请勿用于任何商业行为;如需要,请使用正版授权;侵权联删。推荐最新 IntelliJ I
- 一般情况下每个spring boot工程启动都有固定的端口,但是固定端口不利用服务的动态扩容,如果在一台服务器上需要对同一个服务进行多实例部
- 前言我们来分析一下堆内布局以及Java对象在内存中的布局吧。对象的指向先来看一段代码:package com.zwx.jvm;public
- 1.MediaCodec 是什么MediaCodec类可以访问底层媒体编解码器框架(StageFright 或 OpenMAX),即编解码组
- 问:怎样才能将XML文件导入SQL Server 2000? 答:将XML文件导入SQL Server有若干种方法,这里提供其中的3种: 大
- 为大家分享一些android公共方法native.js实现代代码,如获取手机MAC地址,手机内存大小,手机存储空间大小,手机CPU信息等手机
- 1、Nacos config springboot starter包我们在springboot应用中集成nacos配置中心时,添加了以下依赖
- 目前,比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel直接上代码:一,POIPOI是apa