步骤:
1.新建xml文件
setting_item_cardview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="35dp" app:cardCornerRadius="4dp"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/text_item_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="20dp" android:textSize="14dp" android:textColor="#000000" android:text="设置自动更新" /> <TextView android:id="@+id/text_item_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_item_up" android:layout_marginLeft="20dp" android:layout_marginBottom="10dp" android:textColor="#66000000" android:text="当前自动更新已经关闭" /> <Switch android:id="@+id/switch_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_margin="10dp" /> </RelativeLayout> </androidx.cardview.widget.CardView> </RelativeLayout>2.新建属性xml values目录下
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SwitchView" > <attr name="title" format="string"/> <attr name="switch_on" format="string"/> <attr name="switch_off" format="string"/> </declare-styleable> </resources>3.新建view继承线性或相对布局
SwitchView
package com.example.test; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; import androidx.annotation.Nullable; public class SwitchView extends LinearLayout { TextView text_item_down,text_item_up; Switch switch_item; String title,switchOff; public SwitchView(Context context) { super(context); initView(context); } public SwitchView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "title"); switchOff = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto", "switch_off"); text_item_up.setText(title); text_item_down.setText(switchOff); } private void initView(Context context) { View.inflate(context,R.layout.setting_item_cardview,this); text_item_down=findViewById(R.id.text_item_down); text_item_up= findViewById(R.id.text_item_up); switch_item= findViewById(R.id.switch_item); } public void setSwitchOnClickListener(CompoundButton.OnCheckedChangeListener listener) { switch_item.setOnCheckedChangeListener(listener); } public void setText(String string) { text_item_down.setText(string); } }4.actvity中对应的xml文件中,引用3中定义的view控件,并指定相关属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:maybe="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.test.SwitchView android:id="@+id/slt" android:layout_width="wrap_content" android:layout_height="wrap_content" maybe:switch_off="当前自动更新已经关闭" maybe:switch_on="当前自动更新已经开启" maybe:title="设置自动更新"> </com.example.test.SwitchView> <com.example.test.SwitchView android:layout_width="wrap_content" android:layout_height="wrap_content" maybe:switch_off="这是自定义控件2" maybe:title="这是自定义控件"> </com.example.test.SwitchView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是TextView的控件" android:layout_marginLeft="20dp"/> </LinearLayout>运行效果如下:
参考链接
https://www.jianshu.com/p/1a0e42cca267