Android自定義實(shí)現(xiàn)圖片加文字功能
Android自定義實(shí)現(xiàn)圖片加文字功能
分四步來(lái)寫(xiě):
1,組合控件的xml;
2,自定義組合控件的屬性;
3,自定義繼承組合布局的class類,實(shí)現(xiàn)帶兩參數(shù)的構(gòu)造器;
4,在xml中展示組合控件。
具體實(shí)現(xiàn)過(guò)程:
一、組合控件的xml
我接觸的有兩種方式,一種是普通的Activity的xml;一種是父節(jié)點(diǎn)為merge的xml。我項(xiàng)目中用的是第一種,但個(gè)人感覺(jué)第二種好,因?yàn)榈谝环N多了相對(duì)或者絕對(duì)布局層。
我寫(xiě)的 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>
這里展示一個(gè)merge的例子,有時(shí)間,大家可以自己體會(huì)下。
<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>
這兩個(gè)xml,都是寫(xiě)在layout中的。
二、自定義組合控件的屬性
這步是我們自定義的重要部分之一,自定義組件的私有特性全顯示在這。
首先在values中創(chuàng)建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>
這里有幾點(diǎn)需要注意的,第一:屬性名為name,第二:屬性單位為fromat。這單位包含的值可以查看這里。
三、自定義繼承組合布局的class類,實(shí)現(xiàn)帶兩參數(shù)的構(gòu)造器
我實(shí)現(xiàn)的CustomPicText.Java
/**
* Created by Hman on 2017/5/4.
* 為了測(cè)試自定義組合控件
*/
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);
// 為標(biāo)題設(shè)置屬性
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);
// 為日期設(shè)置屬性
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();
}
}
}
在這里,我們也可以給控件添加一些監(jiān)聽(tīng)器,大家自己去加上;這里值得注意的是一個(gè)加載配置的類TypeArray
4,在xml中展示組合控件
這個(gè)隨便寫(xiě)到一個(gè)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>
這里有一點(diǎn)別忘記,添加xmlns:hman=”http://schemas.Android.com/apk/res-auto”
總結(jié)
程序基本上到這就結(jié)束了。
看下效果截圖

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android動(dòng)畫(huà)之逐幀動(dòng)畫(huà)(Frame Animation)基礎(chǔ)學(xué)習(xí)
大家都知道逐幀動(dòng)畫(huà)是一種常見(jiàn)的動(dòng)畫(huà)形式,其原理是在“連續(xù)的關(guān)鍵幀”中分解動(dòng)畫(huà)動(dòng)作,也就是在時(shí)間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動(dòng)畫(huà)。下面我們就來(lái)學(xué)習(xí)下Android中逐幀動(dòng)畫(huà)的基礎(chǔ)知識(shí),有需要的可以參考借鑒。2016-09-09
Android NDK開(kāi)發(fā)之:配置環(huán)境的詳解
本篇文章是對(duì)Android中的配置環(huán)境進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android中將一個(gè)圖片切割成多個(gè)圖片的實(shí)現(xiàn)方法
有種場(chǎng)景,我們想將一個(gè)圖片切割成多個(gè)圖片。比如我們?cè)陂_(kāi)發(fā)一個(gè)拼圖的游戲,就首先要對(duì)圖片進(jìn)行切割2013-05-05
Android實(shí)現(xiàn)淘寶倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)淘寶倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
自定義RadioButton和ViewPager實(shí)現(xiàn)TabHost帶滑動(dòng)的頁(yè)卡效果
在工作中又很多需求都不是android系統(tǒng)自帶的控件可以達(dá)到效果的所以這個(gè)時(shí)候就要自定義控件來(lái)達(dá)到效果:使用自定義RadioButton和ViewPager實(shí)現(xiàn)TabHost帶滑動(dòng)的頁(yè)卡效果2013-01-01
Android 中使用 ViewPager實(shí)現(xiàn)屏幕頁(yè)面切換和頁(yè)面輪播效果
ViewPager是谷歌官方給我們提供的一個(gè)兼容低版本安卓設(shè)備的軟件包,里面包囊了只有在安卓3.0以上可以使用的api。下面我們就展示下ViewPager可以實(shí)現(xiàn)的兩種簡(jiǎn)單效果,感興趣的朋友一起看看吧2016-12-12
Android jni調(diào)試打印char陣列的實(shí)例詳解
這篇文章主要介紹了Android jni調(diào)試打印char陣列的實(shí)例詳解的相關(guān)資料,通過(guò)此文希望能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
簡(jiǎn)單好用的Adapter---ArrayAdapter詳解
這篇文章主要介紹了簡(jiǎn)單好用的Adapter---ArrayAdapter詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
android實(shí)現(xiàn)節(jié)點(diǎn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)節(jié)點(diǎn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android 常見(jiàn)的圖片加載框架詳細(xì)介紹
這篇文章主要介紹了Android 常見(jiàn)的圖片加載框架詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12

