1.自定义属性resource资源
<declare-styleable name="SettingItemView">
<!-- 定义单个的属性 -->
<!-- title -->
<attr name="sivTitle" format="string|reference" />
<!-- 背景 -->
<attr name="sivBackground">
<enum name="start" value="0" />
<enum name="middle" value="1" />
<enum name="end" value="2" />
</attr>
<!-- 开关显示 -->
<attr name="sivToggle" format="boolean" />
</declare-styleable>
2.自定义View
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SettingItemView extends RelativeLayout {
// 枚举
// <enum name="start" value="0" />
// <enum name="middle" value="1" />
// <enum name="end" value="2" />
private static final int BACKGROUND_START = 0;
private static final int BACKGROUND_MIDDLE = 1;
private static final int BACKGROUND_END = 2;
private TextView mTvTitle;
private ImageView mIvToggle;
private boolean isOpened = true; // 用来记录是否是打开的
public SettingItemView(Context context) {
this(context, null);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
// 挂载xml布局
View.inflate(context, R.layout.view_setting_item, this);
// 初始化view
mTvTitle = (TextView) findViewById(R.id.view_tv_title);
mIvToggle = (ImageView) findViewById(R.id.view_iv_toggle);
// 读取属性值
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
String title = ta.getString(R.styleable.SettingItemView_sivTitle);
int background = ta.getInt(R.styleable.SettingItemView_sivBackground, BACKGROUND_START);
boolean toggle = ta.getBoolean(R.styleable.SettingItemView_sivToggle, true);
ta.recycle();
// 设置title
mTvTitle.setText(title);
// 设置背景
switch (background) {
case BACKGROUND_START:
setBackgroundResource(R.drawable.setting_start_selector);
break;
case BACKGROUND_MIDDLE:
setBackgroundResource(R.drawable.setting_middle_selector);
break;
case BACKGROUND_END:
setBackgroundResource(R.drawable.setting_end_selector);
break;
default:
setBackgroundResource(R.drawable.first_normal);
break;
}
// 设置开关显示
mIvToggle.setVisibility(toggle ? View.VISIBLE : View.GONE);
}
public void toggle() {
// 如果开关是打开的,就关闭,如果是关闭的就打开
isOpened = !isOpened;
mIvToggle.setImageResource(isOpened ? R.drawable.on : R.drawable.off);
}
public boolean isToggleOpened() {
return isOpened;
}
/**
* 设置开关是否打开
*
* @param isOpened
*/
public void setToggleOpened(boolean isOpened) {
this.isOpened = isOpened;
mIvToggle.setImageResource(isOpened ? R.drawable.on : R.drawable.off);
}
}
3.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="12dp"
android:paddingTop="12dp" >
<TextView
android:id="@+id/view_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="设置自动更新"
android:textSize="18sp" />
<ImageView
android:id="@+id/view_iv_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/on" />
</RelativeLayout>
4.自定义view的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bsj="http://schemas.android.com/apk/res/org.bsj.safe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<org.bsj.safe.view.SettingItemView
android:id="@+id/setting_siv_autoupdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
bsj:sivBackground="start"
bsj:sivTitle="自动更新设置"
bsj:sivToggle="true" />
<org.bsj.safe.view.SettingItemView
android:id="@+id/setting_siv_callsmssafe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
bsj:sivBackground="end"
bsj:sivTitle="骚扰拦截设置"
bsj:sivToggle="true" />
<org.bsj.safe.view.SettingItemView
android:id="@+id/setting_siv_numberaddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
bsj:sivBackground="start"
bsj:sivTitle="归属地显示设置"
bsj:sivToggle="true" />
<org.bsj.safe.view.SettingItemView
android:id="@+id/setting_siv_numberaddressstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
bsj:sivBackground="end"
bsj:sivTitle="归属地风格设置"
bsj:sivToggle="false" />
</LinearLayout>
4.效果图