自定义组合控件使用的过程
1 自定义一个View 一般来说 继承相对布局或者线性布局 ViewGroup
2 实现父类的构造方法 一般来说 需要在构造方法里初始化自定义的布局文件
3 根据一些需要或者需求 定义一些API方法
----------------
4 根据需要 自定义控件的属性 可以参照TextView属性
5 自定义命名空间 例如
xmlns:<名称>="http://schemas.android.com/apk/res/<包名>"
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
6 自定义我们的属性 在res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
7 使用我们自定义的属性 例如
itheima:desc_off="设置自动更新已关闭"
itheima:desc_on="设置自动更新已开启"
itheima:title="设置自动更新"
<com.itheima.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="65dip"
itheima:desc_off="设置自动更新已关闭"
itheima:desc_on="设置自动更新已开启"
itheima:title="设置自动更新" />
8 在我们自定义控件带有两个构造参数的构造方法里AttributeSet attrs
取出我们的属性值 关联自定义布局文件对应的控件
效果图如下:
<com.itheima.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="65dip"
itheima:desc_off="设置自动更新已关闭"
itheima:desc_on="设置自动更新已开启"
itheima:title="设置自动更新" />
我们在自定义控件的基础上,对自定义控件进行属性的设置,使其能用。
1 初始化命名空间 itheima
2 加入attrs.xml文件,声明属性内容
3 在自定义控件带有两个参数构造函数的方法里,获取自定义属性的值
4 在自定义控件代码中,使用自定义属性
1 初始化命名空间 itheima
在布局文件开头添加:
xmlns:<自定义命名空间>=" http://schemas.android.com/apk/res/<包名>"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="55dip"
android:background="#8866ff00"
android:gravity="center"
android:text="设置中心"
android:textSize="25sp" />
<com.itheima.mobilesafe.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="65dip"
itheima:desc_off="设置自动更新已关闭"
itheima:desc_on="设置自动更新已开启"
itheima:title="设置自动更新" />
</LinearLayout>
2 加入attrs.xml文件,声明属性内容
在res/values 下添加attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
3 在自定义控件带有两个参数构造函数的方法里,获取自定义属性的值
package com.itheima.mobilesafe.ui;
import com.itheima.mobilesafe.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 SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_title;
private TextView tv_msg;
private String desc_on;
private String desc_off;
private void initView(Context context) {
View view =View.inflate(context, R.layout.setting_item,this);
cb_status =(CheckBox) view.findViewById(R.id.cb_status);
tv_msg =(TextView) view.findViewById(R.id.tv_msg);
tv_title =(TextView) view.findViewById(R.id.tv_title);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
/**
* 带有两个参数的构造方法,布局文件使用调用
* @param context
* @param attrs
*/
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);//使用布局一般调用2个参数构造方法
String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.itheima.mobilesafe", "title");
desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.itheima.mobilesafe", "desc_on");
desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.itheima.mobilesafe", "desc_off");
tv_title.setText(title);
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
/**
* 获取CheckBox当前状态
* @return 返回状态
*/
public boolean isCheckBox(){
return cb_status.isChecked();
}
/**
* 设置CheckBox状态
* @param checked 设置的值
*/
public void setCheckBox(boolean checked){
if(checked){
setStatusMessage(desc_on);
}else{
setStatusMessage(desc_off);
}
cb_status.setChecked(checked);
}
/**
* 设置状态的消息信息
* @param text 设置的内容
*/
public void setStatusMessage(String text){
tv_msg.setText(text);
}
}
4 在自定义控件代码中,使用自定义属性
/**
* 设置CheckBox状态
* @param checked 设置的值
*/
public void setCheckBox(boolean checked){
if(checked){
setStatusMessage(desc_on);
}else{
setStatusMessage(desc_off);
}
cb_status.setChecked(checked);
}