自定义控件layout.xml: java
<?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="70dip" android:orientation="vertical" > <TextView android:id="@+id/tv_settingview_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dip" android:layout_marginTop="3dip" android:text="标题" android:textColor="@color/black" android:textSize="28sp" /> <TextView android:id="@+id/tv_settingview_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_settingview_title" android:layout_marginLeft="3dip" android:layout_marginTop="2dip" android:text="描述信息" android:textColor="#99000000" android:textSize="18sp" /> <CheckBox android:clickable="false" //不能被点击 android:focusable="false" //不能获取焦点 android:id="@+id/cb_settingview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" /> <View android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:background="@drawable/list_devider" android:layout_height="1dip" > </View> </RelativeLayout>
自定义控件类: android
public class SettingView extends RelativeLayout { private View view; private TextView tv_setting_1; private TextView tv_setting_2; private CheckBox cb_setting; 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.my_setting_view); String content = a.getString(R.styleable.my_setting_view_content); String title = a.getString(R.styleable.my_setting_view_title); tv_setting_1.setText(title); tv_setting_2.setText(content); a.recycle(); } public SettingView(Context context) { super(context); initView(context); } private void initView(Context context){ //把xml文件转化成对象,this:view对象以本身为父体 view = View.inflate(context, R.layout.view_setting, this); tv_setting_1 = (TextView) view.findViewById(R.id.tv_setting_1); tv_setting_2 = (TextView) view.findViewById(R.id.tv_setting_2); cb_setting = (CheckBox) view.findViewById(R.id.cb_setting); } /** * 判断是否被选中 * @return */ public boolean isCkecked(){ return cb_setting.isChecked(); } /** * 设置当前控件的选中状态 * @param checked * @return */ public void setChecked(boolean checked){ cb_setting.setChecked(checked); } /** * 更改content里面的内容 * */ public void setContent(String text, int color){ tv_setting_2.setText(text); tv_setting_2.setTextColor(color); } }
value目录下的attrs.xml: 数组
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="my_setting_view"> <attr name="title" format="string" /> <attr name="content" format="string" /> </declare-styleable> </resources>
用到自定义控件的Activity的layout.xml中: app
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:itcast="http://schemas.android.com/apk/res/com.example.myapplication" //自定义控件的名称空间 android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/rl_set" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/lightskyblue" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="我的设置" android:textSize="25sp" /> </LinearLayout> <com.example.myapplication.SettingView itcast:title = "自动更新" itcast:content = "自动更新已经开启" android:id="@+id/sv_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
用到自定义控件的Activity:
public class SettingActivity extends Activity { private SettingView sv_setting; private SharedPreferences sp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); sp = getSharedPreferences("config", MODE_PRIVATE); sv_setting = (SettingView) findViewById(R.id.sv_setting); boolean update = sp.getBoolean("update", true); if(update){ sv_setting.setChecked(true); sv_setting.setContent("自动更新已经开启", Color.BLACK); }else{ sv_setting.setChecked(false); sv_setting.setContent("自动更新没有开启", Color.RED); } sv_setting.setOnClickListener(new OnClickListener() { public void onClick(View v) { Editor editor = sp.edit(); if (sv_setting.isCkecked()) { sv_setting.setChecked(false); sv_setting.setContent("自动更新没有开启", Color.RED); editor.putBoolean("update", false); } else { sv_setting.setChecked(true); sv_setting.setContent("自动更新已经开启", Color.BLACK); editor.putBoolean("update", true); } editor.commit(); } }); } }