步骤
1、创建自定义控件布局xml
2、声明一个View类 继承第1步中最外层的布局的类,一般为相对布局,或者线性布局 或者其他的ViewGroup。
public class SettingTextView extends RelativeLayout {
3、在自定义的View对象里面重写它的构造方法。在构造方法里面就把布局都初始化完毕。
4、根据业务需求 添加一些api方法,扩展自定义的组合控件;
5、可以在布局文件里面 可以自定义一些属性。
自定义属性的步骤:
1、在res目录下的values目录下创建attrs.xml的文件 声明你写的属性。
<declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="update_on" format="string" />
<attr name="update_off" format="string" />
</declare-styleable>
1、使用自定义属性时注意声明自定义属性的命名空间(最后为你项目的包名)。
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
2、在布局文件中使用你自定义的属性。下面的
<com.itheima.mobilesafe.ui.SettingTextView
android:layout_width="match_parent"
android:layout_height="60dip"
android:id="@+id/stv_update"
itheima:update_on="自动更新已经开启"
itheima:update_off="自动更新已经关闭"
itheima:title="设置自动更新"
/>
3、获取这些定义的属性。自定义View对象的构造方法里面 有一个带两个参数的构造方法
布局文件里面定义的属性都放在 AttributeSet attrs
/**
* 系统默认加载布局文件时,创建这个控件对象时调用这个方法
* @param context
*/
public SettingTextView(Context context, AttributeSet attrs) {
<p><span style="white-space:pre"> </span>super(context,attrs);
//获取自定义属性</p><p> title = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "title");update_on = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_on");update_off = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_off");initView(context);}
</p>
实例代码
自定义组合控件布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dip" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="13dip"
android:layout_marginTop="10dip"
android:text="设置自动更新"
android:textColor="#000000"
android:textSize="18sp" />
<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="13dip"
android:layout_marginTop="3dip"
android:text="自动更新已经关闭"
android:textColor="#88000000"
android:textSize="15sp" />
<CheckBox
android:checked="false"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:id="@+id/cb_status"/>
<View
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="0.2dip"
android:background="#66000000"
android:id="@+id/v_line"
/>
</RelativeLayout>
自定义组合控件类
package com.itheima.mobilesafe.ui;
import com.itheima.mobilesafe.R;
import com.itheima.mobilesafe.R.id;
import com.itheima.mobilesafe.R.layout;
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 SettingTextView extends RelativeLayout {
private TextView tv_title;
private TextView tv_desc;
private CheckBox cb_status;
private View v_line;
private String update_on;
private String update_off;
private String title;
/**
* 在布局文件中指定空间时的样式时,系统默认加载布局文件时,创建这个控件对象时调用这个方法
* @param context
*/
public SettingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
/**
* 系统默认加载布局文件时,创建这个控件对象时调用这个方法
* @param context
*/
public SettingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
title = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "title");
update_on = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_on");
update_off = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_off");
initView(context);
}
/**
* 在代码中new这样的对象使用这个方法
* @param context
*/
public SettingTextView(Context context) {
super(context);
initView(context);
}
public void initView(Context context){
View view = View.inflate(context, R.layout.setting_text_view, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_title.setText(title);
tv_desc = (TextView) view.findViewById(R.id.tv_desc);
cb_status = (CheckBox) view.findViewById(R.id.cb_status);
v_line = (View) view.findViewById(R.id.v_line);
}
public void setChecked(boolean checked){
cb_status.setChecked(checked);
if(checked){
tv_desc.setText(update_on);
}else{
tv_desc.setText(update_off);
}
}
public boolean isChecked(){
return cb_status.isChecked();
}
}
布局中使用自定义组合控件
<com.itheima.mobilesafe.ui.SettingTextView
android:layout_width="match_parent"
android:layout_height="60dip"
android:id="@+id/stv_update"
itheima:update_on="自动更新已经开启"
itheima:update_off="自动更新已经关闭"
itheima:title="设置自动更新"
/>
代码中使用自定义组合控件
private SettingTextView stv_update;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
stv_update = (SettingTextView) findViewById(R.id.stv_update);
//回显是否更新的状态
stv_update.setChecked(true);
//添加自定义控件的单击事件的监听器
stv_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//如果点击当前控件,设置复选框选中的状态
if(stv_update.isChecked()){
stv_update.setChecked(false);
}else{
stv_update.setChecked(true);
}
}
});
}
自定义属性xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingTextView">
<attr name="update_on" format="reference" />
<attr name="update_off" format="reference" />
<attr name="title" format="reference" />
</declare-styleable>
</resources>