自定义控件--自定义组合控件(RelativeLayout的组合控件)

自定义组合控件的步骤:

例如设置界面的某一条设置一般由两个TextView,一个checkBox和一个Viw(那条下面的分割线)组成的相对布局构成,而这个组合一般是比较常用的,可以考虑把他们做成一个自定义组合控件。

步骤一:新建一个相对布局的文件update_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <!--该布局文件是一个自定义的控件,可看成是一个整体的控件,由多个控件组成-->
    <TextView
        android:id="@+id/tv_isUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:text="@string/tv_isUpdate_text"/>
    <TextView
        android:id="@+id/tv_isUpdate_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_isUpdate"
        android:textSize="25sp"
        android:textColor="#808080"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:text="@string/tv_isUpdate_state_text"/>
    <!--将CheckBox的clickable属性和focusable的属性设为false的目的是放弃CheckBox自身的属性,因为CheckBox的获取焦点和可点击的优先级-->
    <!--在整个自定义控件中时最高的,因此放弃掉,而只是把CheckBox作为整个自定义控件的一个普通的部分看待,但是保留了勾选和非勾选的样式,为了-->
    <!--提示用户-->
    <CheckBox
        android:id="@+id/ck_checkbox"
        android:clickable="false"
        android:focusable="false"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <View
        android:id="@+id/view_line"
        android:layout_below="@id/tv_isUpdate_state"
        android:layout_width="fill_parent"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="1dp"
        android:background="#808080"/>

</RelativeLayout>

步骤二:新建一个类CustomRelativeLayoutView继承RelativeLayout,在CustomRelativeLayoutView的构造方法中,将刚才自定义的MyRelative.xml文件填充到我自定的MyRelativeView中,View.inflate(context,MyRelative.xml的id,MyRelativeView填充目标);这个方法第三个参数就是填充目标; 这样我定义的xml布局文件就填充到我定义的类中了,我这个类就是我定义的xml文件的样式。假如在组合控件中有一个CheckBox,想实现点击组合控件的任意位置都可以实现选中和取消CheckBox的方法:
思路将CheckBox与这个组合控件整体实现同生共死:在MyRelativeView.java中写一个boolean isChecked();和void setChecked()方法,方法体都是CheckBox的isChecked()方法和setChecked()方法。然后给这个组合控件一个单机事件实现

public class CustomRelativeLayoutView extends RelativeLayout {

    private CheckBox ck_checkBox;
    private TextView is_update_state;

    private void initView(Context context) {
        View.inflate(context, R.layout.update_item, CustomRelativeLayoutView.this);
        ck_checkBox = (CheckBox) this.findViewById(R.id.ck_checkbox);
        is_update_state = (TextView) this.findViewById(R.id.tv_isUpdate_state);
    }
    public CustomRelativeLayoutView(Context context) {
        super(context);
        initView(context);
    }

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

    public CustomRelativeLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomRelativeLayoutView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    /**
     * 将checkBox的选中与否的状态赋予当前自定义控件,
     * 使当前控件与checkbox看成一个整体
     */
    public boolean isChecked(){
        return ck_checkBox.isChecked();
    }

    /**
     * 给当前控件设置是否选中状态,其实就是给CheckBox设置是否选中状态。
     * @param boo
     */
    public void setChecked(boolean boo) {
        ck_checkBox.setChecked(boo);
    }

    /**
     * 是否启用更新的结果
     * @param text
     */
    public void setIsUpdateState(String text){
        is_update_state.setText(text);
    }
}


步骤三:要在某个布局文件中引用我自定的布局文件,就可以引用CustomRelativeLayoutView.java的全类名就行了,可以在指定一下宽高;也就是说此时这个全类名的控件就可以被看成普通的控件来操作了,例如定义一个id,再在activity中findViewById()等等
试试,在xml布局文件中引用自定义控件时,执行了自定义控件的哪个构造方法,其实是一般执行两个参数的构造方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="30sp"
        android:background="#A52A2A"
        android:text="@string/tv_setting_text"/>
    <com.mycompany.mysimple.customrelativelayout.CustomRelativeLayoutView
        android:id="@+id/custom_relative"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
步骤四:Activity中的单击事件
public class CustomRelativeLayoutActivity extends FragmentActivity implements View.OnClickListener{

    private CustomRelativeLayoutView custom_relative;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customrelativelayout);
        custom_relative = (CustomRelativeLayoutView) findViewById(R.id.custom_relative);
        custom_relative.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.custom_relative:
                if(custom_relative.isChecked()){
                    custom_relative.setChecked(false);
                    custom_relative.setIsUpdateState("自动更新已关闭");
                }else{
                    custom_relative.setChecked(true);
                    custom_relative.setIsUpdateState("自动更新已开启");
                }
                break;
            default:
                break;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值