一、效果图



选中:显示自动更新开启 不选择:显示自动更新关闭 ------------ 在布局文件中的使用方式和android自生的控件一样
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        style="@style/ActivityTitle"        android:text="@string/settingTitle" />    <com.zengmg.MobileSafe.view.SettingItemView        android:id="@+id/siv_update"        android:layout_width="match_parent"        android:layout_height="wrap_content"        zengmg:desc_off="自动更新关闭"        zengmg:desc_on="自动更新开启"        zengmg:title="自动更新设置" /></LinearLayout>


二、步骤

思路:模仿android定义好的控件来写
1、写好布局文件 2、编写自定义 attrs.xml 3、编写自定义控件代码 4、在activity中使用,增加点击事件代码

三、各步骤代码

控件名:SettingItemView

3.1布局文件:activity_setting.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        style="@style/ActivityTitle"        android:text="@string/settingTitle" />    <com.zengmg.MobileSafe.view.SettingItemView        android:id="@+id/siv_update"        android:layout_width="match_parent"        android:layout_height="wrap_content"        zengmg:desc_off="自动更新关闭"        zengmg:desc_on="自动更新开启"        zengmg:title="自动更新设置" /></LinearLayout>

注意:需要加入一个自定义的xml声明:
xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"
声明中的 zengmg 是配置属性时的头。
zengmg:desc_off="自动更新关闭"zengmg:desc_on="自动更新开启"zengmg:title="自动更新设置" 

3.2 自定义 attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources>       <declare-styleable name="SettingItemView">        <attr name="title" format="string" />        <attr name="desc_on" format="string" />        <attr name="desc_off" format="string" />   </declare-styleable>      <!--    在D:\ADT\sdk\platforms\android-16\data\res\values\attrs.xml 文件中找 name="TextView"。   name="SettingItemView" 是控件名    -->    </resources>

3.3 自定义控件代码SettingItemView.java
package com.zengmg.MobileSafe.view;import com.zengmg.MobileSafe.R;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.CheckBox;import android.widget.RelativeLayout;import android.widget.TextView;/** * 设置页面的自定义控件 * 布局文件在:view_setting_item.xml * 属性文件在:values/attrs.xml。(通过查看Android自带的控件找到的) *  * @author zengmg *  */public class SettingItemView extends RelativeLayout {private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.zengmg.MobileSafe";private TextView tvTitle;private TextView tvDesc;private CheckBox cbState;private String mtitle;private String mDescOn;private String mDescOff;public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);mtitle=attrs.getAttributeValue(NAMESPACE, "title");mDescOn=attrs.getAttributeValue(NAMESPACE, "desc_on");mDescOff=attrs.getAttributeValue(NAMESPACE, "desc_off");initView();}public SettingItemView(Context context) {super(context);}public void initView(){//View android.view.View.inflate(Context context, int resource, ViewGroup root)//将自定义好的布局文件设置给当前的SettingItemView控件//当SettingItemView控件被使用时,文件view_setting_item里的布局就被显示了View.inflate(getContext(), R.layout.view_setting_item, this);tvTitle=(TextView) findViewById(R.id.tv_title);tvDesc=(TextView) findViewById(R.id.tv_desc);cbState=(CheckBox) findViewById(R.id.cb_status);setMtitle(mtitle);}public String getMtitle() {return tvTitle.getText().toString();}public void setMtitle(String mtitle) {tvTitle.setText(mtitle);}public String getmDescOn() {return tvDesc.getText().toString();}public void setmDescOn(String mDescOn) {tvDesc.setText(mDescOn);}public String getmDescOff() {return tvDesc.getText().toString();}public void setmDescOff(String mDescOff) {tvDesc.setText(mDescOff);}public boolean isChecked() {return cbState.isChecked();}public void setChecked(boolean mIsChecked) {cbState.setChecked(mIsChecked);if(mIsChecked){tvDesc.setText(mDescOn);}else{tvDesc.setText(mDescOff);}}}

有关
View.inflate(getContext(), R.layout.view_setting_item, this);
参见: http://blog.csdn.net/zengmingen/article/details/49975753

3.4 在activity使用的代码

package com.zengmg.MobileSafe.activity;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import com.zengmg.MobileSafe.R;import com.zengmg.MobileSafe.view.SettingItemView;public class SettingActivity extends Activity{private SettingItemView sivUpdate;private SharedPreferences mPref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);mPref = getSharedPreferences("config", MODE_PRIVATE);sivUpdate=(SettingItemView) findViewById(R.id.siv_update);boolean autoUpdate=mPref.getBoolean("auto_update", true);sivUpdate.setChecked(autoUpdate);sivUpdate.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(sivUpdate.isChecked()){//不自动更新sivUpdate.setChecked(false);mPref.edit().putBoolean("auto_update", false).commit();}else{//自动更新sivUpdate.setChecked(true);mPref.edit().putBoolean("auto_update", true).commit();}}});}}





更多相关文章

  1. Android对View的onMeasure方法理解
  2. android 布局之ConstraintLayout的使用
  3. Android(安卓)NDK开发起步Hello Jni
  4. layout 布局
  5. Android(安卓)----可伸缩的控件
  6. 最全面的Android(安卓)Studio使用教程(图文)
  7. android使用html+javascript来制作页面
  8. android中创建XML
  9. Android中xml文件的解析

随机推荐

  1. Android(安卓)SDK Manager更新版慢解决办
  2. android google地图开发总结——获取goog
  3. cocos2d-x eclipse android 配置注意项(
  4. Android作为CXF客户端调用服务端。
  5. [置顶] 近百android开源项目贡献
  6. Android(安卓)创建,验证和删除桌面快捷方
  7. Android(安卓)Audio代码分析13 - AudioTr
  8. Android中build target,minSdkVersion,targ
  9. Android小技巧&Android(安卓)Studio快捷
  10. Android(安卓)与H5交互心得(Base64图片)