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

来源:互联网 发布:em算法 李航 编辑:程序博客网 时间:2024/06/04 18:31

目标:

实现有两个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自定义控件(五) 自定义组合控件

0 0