Android自定義控件如何在XML文件中使用自定義屬性
前言
你好, 我是Cici。這幾天在做一個(gè)小項(xiàng)目的時(shí)候,用到了自定義控件,為了方便在XML中進(jìn)行配置,于是需要用到自定義屬性,特此記下用法,方便復(fù)習(xí)的同時(shí)也希望對(duì)大家有所幫助。
一、為什么需要自定義控件
Android本身提供了很多控件,比如TextView、ImageView等,在實(shí)際開(kāi)發(fā)中,有時(shí)候單個(gè)的控件并不能很好的滿足業(yè)務(wù)需求,因此我們會(huì)將多種控件組合在一起,形成一個(gè)具有特定功能的自定義控件,就好比零件的拼裝,將多個(gè)小零件最后拼成一個(gè)大零件來(lái)使用。
二、具體步驟
1.首先我們創(chuàng)建一個(gè) layout xml文件:
例如:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mi_layout">
<View
android:id="@+id/view_1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/blue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginStart="10dp" />
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/image_1"
app:layout_constraintStart_toEndOf="@id/image_1"
android:layout_marginStart="8dp"
android:text="預(yù)約申請(qǐng)"
android:textSize="14sp"
android:textColor="@color/black" />
<TextView
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/text_1"
app:layout_constraintTop_toBottomOf="@id/text_1"
app:layout_constraintBottom_toBottomOf="@id/image_1"
app:layout_constraintEnd_toStartOf="@id/image_2"
android:layout_marginEnd="66dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:lines="1"
android:text="李老師申請(qǐng)實(shí)驗(yàn)室"
android:textSize="12sp"
android:textColor="@color/gray" />
<androidx.constraintlayout.utils.widget.ImageFilterView
android:id="@+id/image_2"
android:layout_width="8dp"
android:layout_height="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="15dp"
android:layout_marginTop="20dp"
app:roundPercent="1"
android:background="@color/color_f31515" />
<View
android:id="@+id/view_2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
具體效果如下圖所示

2.為自定義控件創(chuàng)建java類:
public class MessageItem extends ConstraintLayout {
private LayoutMessageItemBinding binding;
ConstraintLayout layout;
ImageView iv1;
TextView tv1;
TextView tv2;
ImageView iv2;
private int imageResource1 = R.color.bottom_down;
private int imageResource2 = R.color.color_f31515;
private String text1 = "";
private String text2 = "";
private boolean showSpot = true;
public MessageItem(@NonNull Context context) {
this(context, null);
}
public MessageItem(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MessageItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MessageItem);
imageResource1 = array.getResourceId(R.styleable.MessageItem_imageResource1, imageResource1);
imageResource2 = array.getResourceId(R.styleable.MessageItem_imageResource2, imageResource2);
text1 = array.getString(R.styleable.MessageItem_text1);
text2 = array.getString(R.styleable.MessageItem_text2);
showSpot = array.getBoolean(R.styleable.MessageItem_showSpot, true);
array.recycle();
if (isInEditMode()) {
return;
}
binding = LayoutMessageItemBinding.inflate(LayoutInflater.from(MyApplication.getContext()), this, true);
bindView();
init();
}
private void bindView() {
layout = binding.miLayout;
iv1 = binding.image1;
iv2 = binding.image2;
tv1 = binding.text1;
tv2 = binding.text2;
}
private void init() {
setImageResource1(imageResource1);
setImageResource2(imageResource2);
setText1(text1);
setText2(text2);
setShowSpot(showSpot);
}
private MessageItem setImageResource1(int resId) {
this.iv1.setBackgroundResource(resId);
return this;
}
private MessageItem setImageResource2(int resId) {
this.iv2.setBackgroundResource(resId);
return this;
}
private MessageItem setText1(String text) {
this.tv1.setText(text);
return this;
}
private MessageItem setText2(String text) {
this.tv2.setText(text);
return this;
}
private MessageItem setShowSpot(boolean b) {
this.iv1.setVisibility(b ? VISIBLE : GONE);
return this;
}
}
3.在res/values下,新建一個(gè)attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MessageItem">
<attr name="imageResource1" format="reference" />
<attr name="imageResource2" format="reference" />
<attr name="text1" format="string" />
<attr name="text2" format="string" />
<attr name="showSpot" format="boolean" />
</declare-styleable>
</resources>
4.最后使用:
在使用的時(shí)候,我們只需要在相應(yīng)的 XML 文件中引入即可,例如:
<com.example.lims.widget.MessageItem
android:id="@+id/f_message_i3"
app:text1="維修意見(jiàn)"
app:text2="張同學(xué)向您提出設(shè)備維護(hù)意見(jiàn)"
app:imageResource1="@drawable/message_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/f_message_i2"
app:layout_constraintStart_toStartOf="parent" />
<com.example.lims.widget.MessageItem
android:id="@+id/f_message_i1"
app:text1="預(yù)約申請(qǐng)"
app:text2="王老師向您申請(qǐng)實(shí)驗(yàn)室"
app:imageResource1="@drawable/message_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintStart_toStartOf="parent" />
其中,app:text1、app:text2、app:imageResource1 為自定義屬性。這樣就可以根據(jù)業(yè)務(wù)需要,為我們的自定義控件設(shè)置不同的屬性值,最終得到不同的效果。
以上就是Android自定義控件--如何在XML文件中使用自定義屬性的詳細(xì)內(nèi)容,更多關(guān)于Android XML自定義屬性的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android屏蔽軟鍵盤并且顯示光標(biāo)的實(shí)例詳解
這篇文章主要介紹了Android屏蔽軟鍵盤并且顯示光標(biāo)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10
Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能,結(jié)合具體實(shí)例形式分析了Android多列表顯示功能的相關(guān)布局操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
詳解Android壁紙服務(wù)的啟動(dòng)過(guò)程
你有設(shè)置過(guò)手機(jī)的壁紙嗎,你知道壁紙是什么樣的程序它是怎么在后臺(tái)運(yùn)行的嗎?這篇文章主要介紹了詳解Android系統(tǒng)壁紙服務(wù)的啟動(dòng)過(guò)程2021-08-08
Flutter中如何加載并預(yù)覽本地的html文件的方法
這篇文章主要介紹了Flutter中如何加載并預(yù)覽本地的html文件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android使用ViewPager實(shí)現(xiàn)自動(dòng)輪播
這篇文章主要介紹了Android使用ViewPager實(shí)現(xiàn)自動(dòng)輪播的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
如何使用Flutter實(shí)現(xiàn)手寫(xiě)簽名效果
Flutter插件提供了用于繪制平滑簽名的簽名板,下面這篇文章主要給大家介紹了關(guān)于如何使用Flutter實(shí)現(xiàn)手寫(xiě)簽名效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Android 第三方應(yīng)用接入微信平臺(tái)研究情況分享(一)
微信平臺(tái)開(kāi)放后倒是挺火的,許多第三方應(yīng)用都想試下接入微信這個(gè)平臺(tái),畢竟可以利用微信建立起來(lái)的關(guān)系鏈來(lái)拓展自己的應(yīng)用還是挺不錯(cuò)的 最近由于實(shí)習(xí)需要也在研究這個(gè)東西,這里把我的整個(gè)研究情況給出來(lái)2013-01-01

