android项目自定义组合控件

自定义控件首先要有一个布局文件

<?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="60dp"
    android:padding="5dp" >

    <TextView
        android:id="@+id/tv_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_up"
        android:textColor="#a000"
        android:textSize="14sp" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#a000" 
        android:layout_alignParentBottom="true"/>

    <CheckBox
        android:id="@+id/ck_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

    <TextView
        android:id="@+id/tv_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textColor="#000"
        android:textSize="22sp" />

</RelativeLayout>

然后要写一个类来继承一个父布局,

在里面就可以进行设置

package com.itheima.view;

import com.itheima.superman.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SeetingView extends RelativeLayout{

    private TextView tv_up;
    private TextView tv_down;
    private CheckBox ck_right;
    public SeetingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public SeetingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SeetingView(Context context) {
        super(context);
        initView();
    }
    //初始化布局
    private void initView(){
        //给这个布局一个父控件
        View.inflate(getContext(),R.layout.item_seeting, this);
        tv_up = (TextView) findViewById(R.id.tv_up);
        tv_down = (TextView) findViewById(R.id.tv_down);
        ck_right = (CheckBox) findViewById(R.id.ck_right);
    }
    //设置顶部文字
    public void setUp(String text){
        tv_up.setText(text);
    }
    //设置底部文字
    public void setDown(String text){
        tv_down.setText(text);
    }
    //是否被选中
    public boolean isChecked(){
        return ck_right.isChecked();
    }
    public void setChecked(boolean b){
        ck_right.setChecked(b);
    }
    

}

然后就可以在布局中使用自定义的布局了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/home_bg">

    <TextView 
        style="@style/TitleStyle"
        android:text="设置中心"
        />
    <com.itheima.view.SeetingView
        android:id="@+id/stv_updata"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    

</LinearLayout>

之后再activity里进行操作,和系统控件一样

package com.itheima.superman;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.itheima.view.SeetingView;

public class SeetingActivity extends Activity {
    private SeetingView stv_updata;
    private SharedPreferences mPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seeting);
        mPreferences = getSharedPreferences("config", 0);
        stv_updata = (SeetingView) findViewById(R.id.stv_updata);
        stv_updata.setUp("自动更新设置");
        if (mPreferences.getBoolean("check", true)) {
            stv_updata.setChecked(true);
            stv_updata.setDown("自动更新已经开启");
        } else {
            stv_updata.setChecked(false);
            stv_updata.setDown("自动更新已经关闭");
        }
        stv_updata.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // 选中状态
                if (stv_updata.isChecked()) {
                    stv_updata.setChecked(false);
                    stv_updata.setDown("自动更新已经关闭");
                    mPreferences.edit().putBoolean("check", false).commit();
                } else {
                    stv_updata.setChecked(true);
                    stv_updata.setDown("自动更新已经开启");
                    mPreferences.edit().putBoolean("check", true).commit();
                }

            }
        });
    }
}
原文地址:https://www.cnblogs.com/84126858jmz/p/5001357.html