欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義控件如何在XML文件中使用自定義屬性

 更新時(shí)間:2023年04月07日 09:29:59   作者:Ci_ci  
這篇文章主要為大家介紹了Android自定義控件之如何在XML文件中使用自定義屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

你好, 我是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類(lèi):

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)文章

最新評(píng)論