Android自定義控件如何在XML文件中使用自定義屬性
前言
你好, 我是Cici。這幾天在做一個小項目的時候,用到了自定義控件,為了方便在XML中進(jìn)行配置,于是需要用到自定義屬性,特此記下用法,方便復(fù)習(xí)的同時也希望對大家有所幫助。
一、為什么需要自定義控件
Android本身提供了很多控件,比如TextView、ImageView等,在實(shí)際開發(fā)中,有時候單個的控件并不能很好的滿足業(yè)務(wù)需求,因此我們會將多種控件組合在一起,形成一個具有特定功能的自定義控件,就好比零件的拼裝,將多個小零件最后拼成一個大零件來使用。
二、具體步驟
1.首先我們創(chuàng)建一個 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ù)約申請"
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="李老師申請實(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下,新建一個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.最后使用:
在使用的時候,我們只需要在相應(yīng)的 XML 文件中引入即可,例如:
<com.example.lims.widget.MessageItem
android:id="@+id/f_message_i3"
app:text1="維修意見"
app:text2="張同學(xué)向您提出設(shè)備維護(hù)意見"
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ù)約申請"
app:text2="王老師向您申請實(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自定義屬性的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
提升Android應(yīng)用視覺吸引效果的10個UI設(shè)計技巧
在Android應(yīng)用開發(fā)中,風(fēng)格和設(shè)計或許不是最關(guān)鍵的要素,但它們在決定Android應(yīng)用成功與否上確實(shí)扮演重要的角色,以下是10個Android應(yīng)用的UI設(shè)計技巧,還有個附加技巧,感興趣的朋友可以了解下哦2013-01-01
Android顏色處理SweepGradient掃描及梯度渲染示例
這篇文章主要為大家介紹了Android顏色處理SweepGradient掃描渲染及梯度渲染示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android使用AudioManager修改系統(tǒng)音量的方法
這篇文章主要介紹了Android使用AudioManager修改系統(tǒng)音量的方法,結(jié)合實(shí)例形式分析了AudioManager調(diào)節(jié)音量的常用方法及相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
Android中ViewPager實(shí)現(xiàn)滑動條及與Fragment結(jié)合的實(shí)例教程
ViewPager類主要被用來實(shí)現(xiàn)可滑動的視圖功能,這里我們就來共同學(xué)習(xí)Android中ViewPager實(shí)現(xiàn)滑動條及與Fragment結(jié)合的實(shí)例教程,需要的朋友可以參考下2016-06-06
Android依據(jù)名字通過反射獲取在drawable中的圖片
依據(jù)圖片的名字,通過反射獲取其在drawable中的ID,在根據(jù)此ID顯示圖片,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
android 6.0 權(quán)限授權(quán)方法
今天小編就為大家分享一篇android 6.0 權(quán)限授權(quán)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

