android 自定义控件的style
16lz
2021-01-26
今天看了一下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); }
更多相关文章
- Android视图的标签属性
- 【Gradle】Android(安卓)Gradle 多项目构建
- Android开发之EditText属性详解
- 它们的定义android滑动菜单
- Android自定义控件实战——滚动选择器PickerView
- attrs.xml文件中属性类型format值的格式
- Android控件的一般属性
- Android(安卓)自定义 HorizontalScrollView 打造再多图片(控件)也
- 不继承TabActivity定义TabHost