自定义组合控件的步骤
1. 写一个类 继承 ViewGroup LinearLayout RelativeLayout
package com.example.horimagedemo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class SettingView extends RelativeLayout{
public SettingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SettingView(Context context) {
super(context);
}
}
2. 如果在布局文件里面定义view对象 使用这个view对象的两个参数的构造方法.
如果是在代码里new出来的是用一个参数的构造方法,所以安全起见三个方法都写上3. 定义这个自定义控件里面要显示的内容
View.inflate(context, R.layout.ui_setting_view, this);package com.example.horimagedemo;
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 SettingView extends RelativeLayout {
private TextView tv_title;
private TextView tv_description;
private CheckBox cb;
public SettingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public SettingView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View view = View.inflate(context, R.layout.ui_setting_view, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_description = (TextView) view.findViewById(R.id.tv_descrpiton);
cb = (CheckBox) view.findViewById(R.id.cb_status);
}
}
4. 添加自定义控件的属性. 定义自定义属性的命名空间
在本工程\res\values\下建立attrs.xml文件,系统控件的属性在\sdk\platforms\android-18\data\res\values\attrs.xml5. 声明自定义的属性
<declare-styleable name="SettingView"><attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
观察R文件 生成 attr内部类 生成styleable 数组 所有的attrs
6. 在xml布局文件里面配置 自定义的属性.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.horimagedemo"//test为命名空间,com.example.horimagedemo为工程的包名
<com.example.horimagedemo.SettingView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="wrap_content" test:title="我是标题" test:desc_on="描述开" test:desc_off="描述关" />
7. 在自定义view对象的构造方法里面 获取AttributeSet ,跟我们自定义的属性建立映射关系
核心代码:
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SettingView);
String title = a.getString(R.styleable.SettingView_title);
desc_on = a.getString(R.styleable.SettingView_desc_on);
desc_off = a.getString(R.styleable.SettingView_desc_off);
tv_title.setText(title);
if (isChecked()) {
tv_description.setText(desc_on);
} else {
tv_description.setText(desc_off);
}
a.recycle();
}
总代码:
package com.example.horimagedemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SettingView extends RelativeLayout {
private TextView tv_title;
private TextView tv_description;
private CheckBox cb;
private String desc_on;
private String desc_off;
public SettingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SettingView);
String title = a.getString(R.styleable.SettingView_title);
desc_on = a.getString(R.styleable.SettingView_desc_on);
desc_off = a.getString(R.styleable.SettingView_desc_off);
tv_title.setText(title);
if (isChecked()) {
tv_description.setText(desc_on);
} else {
tv_description.setText(desc_off);
}
a.recycle();
}
public SettingView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View view = View.inflate(context, R.layout.ui_setting_view, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_description = (TextView) view.findViewById(R.id.tv_descrpiton);
cb = (CheckBox) view.findViewById(R.id.cb_status);
}
/**
* 判断当前控件是否被选中
*
* @return
*/
public boolean isChecked() {
return cb.isChecked();
}
public void setChecked(boolean checked) {
cb.setChecked(checked);
if (checked) {
tv_description.setText(desc_on);
tv_description.setTextColor(Color.GREEN);
} else {
tv_description.setText(desc_off);
tv_description.setTextColor(Color.RED);
}
}
}
8. 在自定义组合控件里面 设置 布局文件的数据, 扩展点击事件.
自己发挥9. 在布局文件使用自定义的view对象.
自己发挥