今天看了一下checkboxPreference的实现,对如何引用到checkbox layout的一头雾水,于是好好补习了下style and theme ,也自己实现了一个自定义的preference 。
自定义Radiopreference的两种方法
1. 直接使用 widgetlayout属性指定 layout文件,这个是android 提供的接口 ,这种方式需要在每个radioPreference 中都要引用该layout,不详细记录。
2. 模仿checkboxPreference方式 定义控件。 步骤: 1. 在attrs.xml 中定义 该 控件对应的style名称,这个名称应该与theme中定义的相同,在checkbox的例子中就是 R.attr.checkBoxPreferenceStyle , 这里我们命名为radioPreferenceStyle
<? xml version = "1.0" encoding = "utf-8" ?> <resources>
< declare-styleable name = "RadioPreference" > < attr name = "radioPreferenceStyle" format = "reference" /> < attr name = "radioPreference_summaryOn" format = "reference" /> < attr name = "radioPreference_summaryoff" format = "reference" /> </ declare-styleable >
</ resources >
2. 在 values/themes.xml中定义theme ,theme中增加 item 。该item的名字与attrs中的对应
<?xmlversion="1.0"encoding="utf-8"?> <resources> <stylename="AppTheme"parent="android:Theme.Light"> <itemname="radioPreferenceStyle">@style/preference_radioButton</item> </style> </resources>
3. 在values/styles.xml中定义具体的radioPreference 属性. 其实也是使用的widgetlayout 属性.
<resourcesxmlns:android="http://schemas.android.com/apk/res/android"> <stylename="preference_radioButton"> <itemname="android:widgetLayout">@layout/preference_radionbutton</item> </style> </resources>
4. 在radioPreference中加载
publicRadioPreference(Context context, AttributeSet attrs,intdefStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioPreference, defStyle, 0); a.recycle(); } publicRadioPreference(Context context, AttributeSet attrs) { //radioPreferenceStyle 定义了radiopreference的样式引用。 this(context, attrs,R.attr.radioPreferenceStyle); } publicRadioPreference(Context context) { this(context,null); }

更多相关文章

  1. Android视图的标签属性
  2. 【Gradle】Android(安卓)Gradle 多项目构建
  3. Android开发之EditText属性详解
  4. 它们的定义android滑动菜单
  5. Android自定义控件实战——滚动选择器PickerView
  6. attrs.xml文件中属性类型format值的格式
  7. Android控件的一般属性
  8. Android(安卓)自定义 HorizontalScrollView 打造再多图片(控件)也
  9. 不继承TabActivity定义TabHost

随机推荐

  1. 详解Centos7 修改mysql指定用户的密码
  2. MySQL5.7安装过程并重置root密码的方法(sh
  3. 微信昵称带符号导致插入MySQL数据库时出
  4. Mysql 忘记root密码和修改root密码的解决
  5. mysql 5.7.12 winx64手动安装教程
  6. SQL分页查询存储过程代码分享
  7. MySQL5.7 group by新特性报错1055的解决
  8. CentOs7.x安装Mysql的详细教程
  9. Mac下忘记mysql密码重新设置密码的图文教
  10. mysql limit 分页的用法及注意要点