自定义属性

来源:互联网 发布:linux怎么改文件夹名字 编辑:程序博客网 时间:2024/06/05 08:39

android 系统定义的属性的查找路径;
E:\ProgramFiles\android-sdk\platforms\android-16\data\res\values \attrs.xml 文件中。
例如:TextView

 <declare-styleable name="TextView">  //声明这个样式的名字:<attr name="text" format="string" localization="suggested" />  //属性,text<attr name="hint" format="string" />  //hint  属性, <attr name="textSize" />  //设置字体的大小。

自定义属性在Values文件夹下,定义个attrs.xml:

<resources>    <declare-styleable name="TextView">         <attr name="title" format="string" />         <attr name="desc_on" format="string" />         <attr name="desc_off" format="string" />        </declare-styleable></resources>

命名空间:
xmlns:com.zh =”http://schemas.android.com/apk/res/com.zh.mobilesafe” 这个是自定义的命名空间,自定义名字和后面的包名,方便在后面能调用自定义的属性。
注意的是自定义的属性,在eclipse下是不能自动感应的,自己手动的敲。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:com.zh="http://schemas.android.com/apk/res/com.zh.mobilesafe" //自己添加的命名空间。    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:orientation="vertical" >    <TextView        style="@style/TitleStyle"        android:text="设置中心" />    <com.zh.mobilesafe.view.SettingItemView        android:id="@+id/siv_update"        android:layout_width="match_parent"        android:layout_height="wrap_content"        com.zh:desc_off="自动更新已关闭" //添加自定义的属性,        com.zh:desc_on="自动更新已开启"        com.zh:title="自动更新设置" /></LinearLayout>

自定义属性出现的问题,注意出现问题时,注意代码的执行顺序。代码是一行行的执行的。

mTitle = attrs.getAttributeValue(NAMESPACE, “title”);
mDescon = attrs.getAttributeValue(NAMESPACE, “desc_on”);
mDescoff = attrs.getAttributeValue(NAMESPACE, “desc_off”);
initView(); //先执行初始化时,mTitle 为空。

自定义属性 自定义后在自定义View中 有属性时才调用的方法中,取得定义属性的值:

//这个是自定义的 View。  也可以称之为自定义的组合控件。package com.zh.mobilesafe.view;import com.zh.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;public class SettingItemView extends RelativeLayout {    private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.zh.mobilesafe";    private TextView tv_title;    private TextView tv_desc;    private CheckBox cb_status;    private String mTitle;    private String mDescon;    private String mDescoff;    // 有定义的style时执行    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        initView();    }    // 有自己的属性时执行      public SettingItemView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        mTitle = attrs.getAttributeValue(NAMESPACE, "title");  //根据属性的名字获得属性的值。一句命名空间的名字namespace;        mDescon = attrs.getAttributeValue(NAMESPACE, "desc_on");        mDescoff = attrs.getAttributeValue(NAMESPACE, "desc_off");        initView();   //初始化的方法需放到最后边执行,否则出现问题是没有mTitle的值。。    }    // 用代码new 对象时    public SettingItemView(Context context) {        super(context);        // TODO Auto-generated constructor stub        initView();    }    /**     * 初始化布局;     */    private void initView() {        // view 中有getContext 方法获得context对象。        // 将自定义的布局文件,设置给当前的SettingItemView        View.inflate(getContext(), R.layout.view_setting_item, this);        tv_title = (TextView) findViewById(R.id.tv_title);        tv_desc = (TextView) findViewById(R.id.tv_desc);        cb_status = (CheckBox) findViewById(R.id.cb_status);        setTitle(mTitle);    }    public void setTitle(String title) {        tv_title.setText(title);    }    public void setDesc(String desc) {        tv_desc.setText(desc);    }    /**     * 判断是否勾选,     *      * @return 返回勾选的状态。     */    public boolean isChecked() {        return cb_status.isChecked();    }    //设置checkbox的勾选    public void setChecked(boolean check){        cb_status.setChecked(check);//根据传入的boolean值判断其是否需要勾选。        //根据选择的状态,设定描述的值。。         if(check){             setDesc(mDescon);         }else{             setDesc(mDescoff);         }    }}

在其他地方调用时:

package com.zh.mobilesafe.activity;import com.zh.mobilesafe.R;import com.zh.mobilesafe.view.SettingItemView;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;/** * 这个是设置中心的页面; *  * @author zh * */public class SettingActivity extends Activity {    private SettingItemView siv_update;// 设置是否升级。 根据checkbox的判断。    private SharedPreferences mPref;     @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_setting);        mPref = getSharedPreferences("config",MODE_PRIVATE);        siv_update = (SettingItemView) findViewById(R.id.siv_update);        //这是设置的默认的勾选状态。        boolean autoUpdate = mPref.getBoolean("auto_update", true);//这个是获得在sp中存储的布尔而值,默认值是true;        if(autoUpdate){            siv_update.setChecked(true);        }else{            siv_update.setChecked(false);        }        siv_update.setOnClickListener(new OnClickListener() {            @Override // 判断siv_update 是否处勾选状态;            public void onClick(View v) {                // TODO Auto-generated method stub                if (siv_update.isChecked()) {                    // 设置勾选,那么设置的时候  就改为不在勾选。                    siv_update.setChecked(false);                    //当设置为不勾选时                    //                    mPref.edit().putBoolean("auto_update", false).commit();                }                 else{                     //如果没有勾选,那么就设置为勾选状态。                     siv_update.setChecked(true);                     //当设置为勾选状态时                     mPref.edit().putBoolean("auto_update", true).commit();                }            }        });    }}
0 0
原创粉丝点击