原生Camera设置的分析(三):自定义控件ListPreference分析

来源:互联网 发布:作曲编曲软件 编辑:程序博客网 时间:2024/05/22 23:58
 


      分析自定义控件ListPreference前,首先来看下ListPreference所用到的一些自定义的属性。关于自定义属性在之前有所提到:Camera学习笔记(一):camera_preferences.xml分析 。

      ListPreference所用到的一些自定义的属性定义如下,一般目录在res/values/attrs.xml:

<declare-styleable name="ListPreference">        <attr name="key" format="string" />        <attr name="defaultValue" format="string|reference" />        <attr name="entryValues" format="reference" />        <attr name="entries" format="reference" /></declare-styleable>

     上面的自定义属性包括主键(key)、默认值(defaultValue)、值的集合(entryValue)和值对应的名字(entries)。

      自定义控件ListPreference的关键代码构造函数如下:

private final String mKey; //主键private final CharSequence[] mDefaultValues; //默认值private CharSequence[] mEntries;private CharSequence[] mEntryValues;private CharSequence[] mOriginalSupportedEntries;private CharSequence[] mOriginalSupportedEntryValues; //记录过滤的分辨率private CharSequence[] mOriginalEntries;private CharSequence[] mOriginalEntryValues; //记录应用层所支持的所有分辨率 public ListPreference(Context context, AttributeSet attrs) {    super(context, attrs);    TypedArray a = context.obtainStyledAttributes(            attrs, R.styleable.ListPreference, 0, 0);    mKey = a.getString(R.styleable.ListPreference_key);    int attrDefaultValue = R.styleable.ListPreference_defaultValue;    TypedValue tv = a.peekValue(attrDefaultValue);    if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {        mDefaultValues = a.getTextArray(attrDefaultValue);    } else {        mDefaultValues = new CharSequence[1];        mDefaultValues[0] = a.getString(attrDefaultValue);    }    setEntries(a.getTextArray(R.styleable.ListPreference_entries));    setEntryValues(a.getTextArray(R.styleable.ListPreference_entryValues));    a.recycle();        mOriginalEntryValues = mEntryValues;    mOriginalEntries = mEntries;}

      setEntryValue()是用来读取应用层所支持的分辨率。例如下面的是某个xml文件用到的该控件,代码如下:

<ListPreference            camera:key="pref_camera_picturesize_key"            camera:title="@string/pref_camera_picturesize_title"            camera:entries="@array/pref_camera_picturesize_entries"            camera:entryValues="@array/pref_camera_picturesize_entryvalues" />

则 setEntryValues 里面的参数 R.styleable.ListPreference_entryValues 是为了获取ListPreferences的属性 entryValues,然后再用 a.getTextArray(R.styleable.ListPreference_entryValues)获取所有的分辨率。


private final String mKey; //主键private final CharSequence[] mDefaultValues; //默认值private CharSequence[] mEntries;private CharSequence[] mEntryValues;private CharSequence[] mOriginalSupportedEntries;private CharSequence[] mOriginalSupportedEntryValues; //记录过滤的分辨率private CharSequence[] mOriginalEntries;private CharSequence[] mOriginalEntryValues; //记录应用层所支持的所有分辨率 public ListPreference(Context context, AttributeSet attrs) {    super(context, attrs);    TypedArray a = context.obtainStyledAttributes(            attrs, R.styleable.ListPreference, 0, 0);    mKey = a.getString(R.styleable.ListPreference_key);    int attrDefaultValue = R.styleable.ListPreference_defaultValue;    TypedValue tv = a.peekValue(attrDefaultValue);    if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {        mDefaultValues = a.getTextArray(attrDefaultValue);    } else {        mDefaultValues = new CharSequence[1];        mDefaultValues[0] = a.getString(attrDefaultValue);    }    setEntries(a.getTextArray(R.styleable.ListPreference_entries));    setEntryValues(a.getTextArray(R.styleable.ListPreference_entryValues));    a.recycle();        mOriginalEntryValues = mEntryValues;    mOriginalEntries = mEntries;}


 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6白屏了怎么办 苹果6s听筒掉漆怎么办 苹果6splus跳屏怎么办 蘑菇街发货好慢怎么办 买手机不给开票怎么办 泰迪狗皮肤病怎么办 狗狗皮肤病严重怎么办? 钢梁高厚比超限怎么办 手机壳按键难按怎么办 层间位移角超限怎么办 淘宝店排名靠后怎么办 如果地震了你会怎么办 地震来了怎么办60字 如果迷路了你会怎么办 吃多了抽烟想吐怎么办 晚上牙疼得要命怎么办 楼梯被火封锁后怎么办 牙齿疼怎么办能快速不疼 我被短信轰炸了怎么办 火警响了在家该怎么办 痔疮肉球变大了怎么办 痔疮长了好几个怎么办 苹果7手机丢了怎么办 如果油锅着火了怎么办 家里电气著火了怎么办 你家油锅起火了怎么办 交通事故后对方不肯去处理怎么办 租的车出了事故怎么办 借的车出了事故怎么办 台风来了怎么办小知识 台中班台风来了怎么办 地震时在五楼怎么办 小事故对方不来怎么办 发生交通事故对方不处理怎么办 当地震来了该怎么办 地震来了该怎么办教案 地震来了怎么办的问题 住30楼的地震了怎么办 在家里地震来了怎么办? 乐高地震来了怎么办? 地震来了该怎么办300字