如何在Android中实现一个在图片中添加文字功能

  介绍

这篇文章给大家介绍如何在Android中实现一个在图片中添加文字功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

<强> Android自定义实现图片加文字功能

<强>分四步来写:

1,组合控件的xml;
2,自定义组合控件的属性;
3,自定义继承组合布局的类类,实现带两参数的构造器;
4,在xml中展示组合控件。

<强>具体实现过程:

<强>一、组合控件的xml

我接触的有两种方式,一种是普通的活动的xml;一种是父节点为xml合并的。我项目中用的是第一种,但个人感觉第二种好,因为第一种多了相对或者绝对布局层。

我写的custom_pictext。xml

& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; RelativeLayout xmlns: android=?;   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"比;      & lt; ImageView   android: id=癅 + id/custom_pic_iv"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android:背景=癅mipmap/a"/比;      & lt; TextView   android: id=癅 + id/custom_date_tv"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_alignBottom=癅 id/custom_pic_iv"   android: layout_marginBottom=? dp"   android: layout_marginLeft=? dp"   android:文本=?017”;/比;      & lt; TextView   android: id=癅 + id/custom_text_tv"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_below=癅 id/custom_pic_iv"   android: layout_marginLeft=? dp"   android: layout_marginTop=? dp"   android:文本=疤饽俊?/比;   & lt;/RelativeLayout>

这里展示一个合并的例子,有时间,大家可以自己体会下。

& lt;合并xmlns: android=癶ttp://schemas.android.com/apk/res/android"比;      & lt;按钮   android: id=癅 + id/title_bar_left"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_alignParentLeft=皌rue"   android: layout_centerVertical=皌rue"   android: layout_marginLeft=? dp"   android:背景=癅null"   android: minHeight=?5 dp"   android: minWidth=?5 dp"   android: textSize=?4 sp"/比;      & lt; TextView   android: id=癅 + id/title_bar_title"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_centerInParent=皌rue"   android:单行模式=皌rue"   android: textSize=?7 sp"/比;      & lt;按钮   android: id=癅 + id/title_bar_right"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   android: layout_alignParentRight=皌rue"   android: layout_centerVertical=皌rue"   android: layout_marginRight=? dp"   android:背景=癅null"   android: minHeight=?5 dp"   android: minWidth=?5 dp"   android: textSize=?4 sp"/比;      & lt;/merge>

这两个xml,都是写在布局中的。

<强>二、自定义组合控件的属性

这步是我们自定义的重要部分之一,自定义组件的私有特性全显示在这。

首先在值中创建attrs。xml

然后定义属性,如下代码

& lt;及# 63;xml version=?.0”;编码=癠TF-8",# 63;比;   & lt; resources>   & lt; declare-styleable name=癈ustomPicText"祝辞   & lt; attr name=皃ic_backgroud"格式=皉eference"/比;   & lt; attr name=皃ic_backgroud_width"格式=癲imension"/比;   & lt; attr name=皃ic_backgroud_height"格式=癲imension"/比;   & lt; attr name=皃ic_text"格式=皊tring"/比;   & lt; attr name=皃ic_text_color"格式=癱olor"/比;   & lt; attr name=皃ic_text_size"格式=癷nteger"/比;   & lt; attr name=皃ic_date"格式=皊tring"/比;   & lt; attr name=皃ic_date_color"格式=癱olor"/比;   & lt; attr name=皃ic_date_size"格式=癷nteger"/比;   & lt;/declare-styleable>      & lt;/resources>

这里有几点需要注意的,第一:属性名为名字,第二:属性单位为fromat。这单位包含的值可以查看这里。

<强>三、自定义继承组合布局的类类,实现带两参数的构造器

我实现的CustomPicText。Java

/* *   *由Hman> & lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比;   http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?;   xmlns:工具=癶ttp://schemas.android.com/tools"   xmlns: hman=癶ttp://schemas.android.com/apk/res-auto"   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"比;      & lt; com.eastsun.widget.CustomPicText   android: id=癅 + id/至上”;   android: layout_width=皐rap_content"   android: layout_height=皐rap_content"   hman: pic_backgroud=癅mipmap/b"   hman: pic_date=?017/5/6"   hman: pic_date_color=癅color/white"   hman: pic_text=暗谝徽磐计?   hman: pic_text_color=癅color/red"   hman: pic_text_size=?8“祝辞& lt;/com.eastsun.widget.CustomPicText>      null

如何在Android中实现一个在图片中添加文字功能