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
0
投稿
猜你喜欢
- spring boot实现自动输出word文档功能本文用到Apache POI组件组件依赖在pom.xml文件中添加<dependen
- 我就废话不多说了,大家还是直接看代码吧~/** * 从list中随机抽取元素 * * @param
- 游标查询(scroll)简介scroll 查询 可以用来对 Elasticsearch 有效地执行大批量的文档查询,而又不用付出深度分页那种
- 序本文主要研究下在带有lombok(1.16.20版本)注解的代码在java10下的编译问题。问题Fatal error compiling
- 前言:Springboot的出现极大的简化了开发人员的配置,而这之中的一大利器便是springboot的starter,starter是sp
- 前言纸上得来终觉浅,绝知此事要躬行一.会话引入什么是会话?会话用来识别不同的客户端,客户端和服务器之间发生的一系列连续的请求和响应的过程,当
- 第一篇是纯利用现有JDK提供的绘图类(ImageIO)类制作,这个过程比较复杂且需要了解ImageIO类。这一篇文章是利用Hutool工具类
- 使用idea的file-》settings-》plugins安装maven helper插件失败,安装页面总是提示installed,在in
- 使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址、文本等)生成二维码并放在该位置,最后加上立牌编号。步骤:1).
- 本文章牵涉到的技术点比较多:spring Data JPA、Redis、Spring MVC,Spirng Cache,所以在看这篇文章的时
- android线程消息机制主要由Handler,Looper,Message和MessageQuene四个部分组成。平常在开发中,我们常用来
- 在用HTML5做跨平台应用开发时,尝尝会用到java和js方法互调的问题,对初学者而言,可能会有点难,在这里分享一些自己在实际开发过程中的用
- 总体实现思路是启动一个生产者项目注册, 将所含服务注册到zookeeper的注册中心, 然后在启动一个消费者项目,将所需服务向zookeep
- MyBatis查询数据赋值给List集合数据缺少今天在使用MyBatis查询数据时,发现查出来的数据和List集合的大小不一致,如下图所示,
- mport java.text.DecimalFormat; DecimalFormat &nb
- java遍历json字符串,取得相应KV值时,各种麻烦,比如将json中的list取出来转为JSONArray,再将list中的object
- 目前市面上流行的爬虫以python居多,简单了解之后,觉得简单的一些页面的爬虫,主要就是去解析目标页面(html)。那么就在想,java有没
- 图像的旋转需要调用 Graphics2D 类的rota
- MyBatis在SQL语句中获取list大小需求:使用MyBatis进行开发时,在一个SQL语句中需要拼接list的大小。大家都知道,当我们
- 本文实例讲述了java实现的简单猜数字游戏代码。分享给大家供大家参考。具体代码如下:import java.util.InputMismat