1. 新建com.example.demo.ui.SwitchUi类
public class SwitchUi extends RelativeLayout {
private TextView tv_tilte;
private TextView tv_desc;
private CheckBox cb_option;
private String desc_on;
private String desc_off;
private String title;
SwitchUi (Context context, AttributeSet attrs){
View v=View.inflate(context, R.layout.ui_switch, this);
tv_title=v.findViewById(R.id.tv_title);
tv_desc=v.findViewById(R.id.tv_desc);
cb_option=v.findViewById(R.id.cb_option);
title=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “title”);
desc_on=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “desc_on”);
desc_off=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “desc_off”);
tv_title.setText(title);
tv_desc.setText(desc_off);
return v;
}
private boolean isChecked(){
return cb_option.isChecked();
}
private boolean setChecked(boolean b){
if (b){
tv_desc.setText(desc_on);
cb_option.setChecked(true);
} else {
tv_desc.setText(desc_off);
cb_option.setChecked(false);
}
}
}
2.res/layout内新建ui_switch.xml文件
<RelativeLayout
android:layout_height=”wrap_content”
android:layout_width=”match_parent>
<TextView
android:layout_id=”@+id/tv_title”
android:layout_marginTop=”5dip”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:layout_textSize=”20sp”
android:layout_textColor=”#000000”/>
<TextView
android:layout_id=”@+id/tv_desc”
android:layout_marginTop=”2dip”
android:layout_below=”@id=tv_title”
android:layout_height=”wrap_content”
android:layout_width=”match_content”
android:layout_textSize=”14sp”
android:layout_textColor=”#99880000”/>
<CheckBox
android:layout_align_bottom_to_parent=”true”
android:layout_align_right_to_parent=”true”
android:layout_id=”@+id=cb_option”
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
/>
</RelativeLayout>
3. res/values内新建attrs.xml
<resources>
<declare-styleable>
<attr name=”SwitchUi”>
< item name=”desc_on” type=”string”/>
< item name=”desc_off” type=”string”/>
< item name=”title” type=”string”/>
</attr>
</declare-styleable>
</resources>
4.新建com.examlpe.demo.activities.MainActivity
public class MainActivity extends activity extends OnClickListener{
private SwitchUi su_light;
private SwitchUi su_computer;
oncreat(){
setContentView(R.layout.activity_main);
su_light=findViewById(R.id.su_light);
su_computer=findViewById(R.id.su_computer);
}
@override
OnClick(View v){
switch(v.getId()){
case R.id.su_light:
if (su_light.isChecked()){
su_light.setChecked(false);
} else {
su_light.setChecked(true);
}
break;
case R.id.su_computer:
if (su_computer.isChecked()){
su_computer.setChecked(false);
} else {
su_computer.setChecked(true);
}
break;
}
}
}
5. layout中新建activity_main.xml
<xmlns:example=android….res/com.example.demo>
<LinearLayout>
<com.example.demo.ui.SwitchUi
android:layout_id=”@+id/su_light”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
example:title=”this is a light”
example:desc_on=”light on !”
example:desc_off=”light off!”>
</com.example.demo.ui.SwitchUi>
<com.example.demo.ui.SwitchUi
android:layout_id=”@+id/su_computer”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
example:title=”this is a computer”
example:desc_on=”computer start !”
example:desc_off=”computer poweroff !”>
</com.example.demo.ui.SwitchUi>
</LinearLayout>
6.manifest中配置
<activity name=”com.example.demo.MainActivity”></activity>