手机安全卫士学习之自定义组合控件

目标:

实现有两个TextView,还有一个CheckBox,还有一个View的组合

自定义组合控件的步骤

1.新建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="68dip" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="8dip"
            android:text="设置是否自动更新"
            android:textColor="#000000"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:layout_marginLeft="10dip"
            android:text="自动更新已经关闭"
            android:textColor="#88000000"
            android:textSize="18sp" />

        <CheckBox
            android:focusable="false"
            android:clickable="false"
            android:id="@+id/cb_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dip"
            android:gravity="center_horizontal" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.2dip"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:background="#000000" />

</RelativeLayout>

2.新建一个组合控件的自定义类SettingItemView
该类的父类就是组合控件根元素对应的类,这里是RelativeLayout,所以这个SettingItemView就应该派生自RelativeLayout。
RelativeLayout有三个构造函数,在SettingItemView类中我们必须重写父类的构造函数

public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

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

    public SettingItemView(Context context) {
        super(context);
        initView(context);
    }
    /**
     * 
     * 初始化布局文件
     * @param context
     */
    private void initView(Context context)
    {
        //把一个布局文件-->View ,并且加载在SettingItemView 
        View.inflate(context, R.layout.setting_item_view, this);

        cb_status = (CheckBox) this.findViewById(R.id.cb_status);

        tv_desc = (TextView) this.findViewById(R.id.tv_desc);
        tv_title = (TextView) this.findViewById(R.id.tv_title);


    }

如上就为自定义控件类搭好了框架,具体需要进行何种操作,就往里面写操作就可以了,比如添加控件点击事件等等。

使用自定义组合控件

 <com.wuyi.mobilesafe.ui.SettingItemView
         android:id="@+id/siv_update"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >


    </com.wuyi.mobilesafe.ui.SettingItemView>

参考博客 Android 自定义组合控件小结
参考博客 android自定义控件(五) 自定义组合控件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值