android custom combination control

---------------setting_item---------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp">
    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/tv_settting_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="Set whether to update automatically"/>

    <TextView
        android:id="@+id/tv_setting_item_subtitle"
        android:layout_below="@+id/tv_settting_item_title"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subtitle"
        android:textSize="18sp"/>
    //The following focusable and clickable are inherent attributes of chekbox. The priority of clicking is higher than that of the combination control, so let's set the click event for the combination control, just remove the click event of the checkbox.
    <CheckBox
        android:focusable="false"
        android:clickable="false"
        android:id="@+id/ck_setting_item"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<View
    android:id="@+id/tv_setting_item_line"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:layout_marginRight="1dp"/>
</RelativeLayout>
/**
 * Created by lambo on 2018/4/15.
 * Custom combination controls---composed of multiple controls, which can be reused
 */

public class Setting_item extends RelativeLayout {
CheckBox cb;
    private TextView tv_subtile;
    private TextView tv_title;
    private String descoff;
    private String descon;
    private String title;

    //Constructor with one parameter, called when initializing the layout file
    public Setting_item(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        //Convert the layout file to view
        View.inflate(context, R.layout.setting_item, this);
        cb=findViewById(R.id.ck_setting_item);
        tv_subtile = findViewById(R.id.tv_setting_item_subtitle);
        tv_title = findViewById(R.id.tv_settting_item_title);
    }
/*Constructor method with two parameters, called when the layout file is used*/
    public Setting_item(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        //Get the attributes title, descon, descoff under the namespace (http://schemas.android.com/apk/res-auto), and extend the attributes to textview in attr.xml
        title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","title");
        descon = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descon");
        descoff = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descoff");
        tv_title.setText(title);
    }

    public Setting_item(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
//Check if the combo control is selected
    public Boolean isChecked(){

        return cb.isChecked();
    }

    //Set the combo control
    public void setChecked(Boolean isChecked){
        if(isChecked){
            setSubtile (descon);
        }else {
            setSubtile (descoff);
        }
        cb.setChecked(isChecked);
    }

    / / Set the method of the combination control from the title
    public void setSubtile(String  subtilte){

tv_subtile.setText(subtilte);
    }
}

---------------------res/values/attr.xml-----------------Add attribute

<?xml version="1.0" encoding="utf-8"?>

<resources>
    //Extend properties to the custom combination control Setting_item
    <declare-styleable name="Setting_item">
        <attr name="title" format="string"/>
        <attr name="descoff" format="string"/>
        <attr name="descon" format="string"/>
    </declare-styleable>


</resources>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607934&siteId=291194637