android 4.2 自定义PreferenceCategory PreferenceScreen Preference等
来源:互联网 发布:淘宝卖花茶要什么手续 编辑:程序博客网 时间:2024/05/08 15:46
需求:
4.2 原生的catagory通常 是一个title, 一条线, 通常是黑色的或白色的线, 现在有一个需求是 改变title的颜色,改变横线颜色的值。
1. 先定义这个category 的布局(布局里没有设置颜色值, 我们将在代码中设置颜色, 当然, 在 xml里也可以设置颜色):
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/preference_title" android:layout_marginTop="1dip" android:layout_marginBottom="1dip" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:id="@+id/preference_divider" android:layout_width="match_parent" android:layout_height="1dip" /></LinearLayout>
2. 给自定义的 category 自定义两个属性, 一个是标题, 一个是分隔线:
<declare-styleable name="PreferenceStyle"> <attr name="divider" format="dimension" /> <attr name="title" format="string" /> </declare-styleable>
3. 自定义ContactsPreferenceCategory :
public class ContactsPreferenceCategory extends PreferenceCategory { private String mTitle; private int mColor; public ContactsPreferenceCategory(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PreferenceStyle); mColor = a.getInt(R.styleable.PreferenceStyle_divider, getContext().getResources().getColor(R.color.call_log_voicemail_highlight_color)); mTitle = a.getString(R.styleable.PreferenceStyle_title); a.recycle(); } protected View onCreateView(ViewGroup parent) { return LayoutInflater.from(getContext()).inflate(R.layout.contacts_category, parent, false); } @Override protected void onBindView(View view) { Log.i("huangbozhi", "class:" + view.getClass()); super.onBindView(view); //在这里设置颜色值和字体大小 TextView tv = (TextView) view.findViewById(R.id.preference_title); tv.setTextSize(16); tv.setTextColor(getContext().getResources().getColor( R.color.call_log_voicemail_highlight_color)); tv.setText(mTitle); //设置线的颜色 View divider = (View) view.findViewById(R.id.preference_divider); divider.setBackgroundColor(mColor); }
4. 在preference 里引用自定义的category, 注意命名空间, 我这里写的是 xmlns:preference="http://schemas.android.com/apk/res/com.android.contacts",因为我的包名是com.android.contacts, 所以引用属性就引用为preference:title
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:preference="http://schemas.android.com/apk/res/com.android.contacts"><com.android.contacts.preference.ContactsPreferenceCategory preference:title="@string/about"> <Preference android:key="help" android:title="@string/help" > </Preference></com.android.contacts.preference.ContactsPreferenceCategory></PreferenceScreen>5. category 对比(其实没改变什么, 就是把颜色改了而已):
0 0
- android 4.2 自定义PreferenceCategory PreferenceScreen Preference等
- Preference的使用(2) --- PreferenceCategory && PreferenceScreen
- [android]隐藏PreferenceCategory和Preference的方法
- Preference,PreferenceCategory,PreferenceList,PreferenceCheckBox等控件的简单讲解
- 使用 Android PreferenceScreen 偏好显示类(android.preference.PreferenceScreen)
- android PreferenceScreen preference偏好显示类的使用介绍
- Android自定义PreferenceCategory的title颜色、字体大小写的方法
- Android自定义PreferenceCategory的title颜色、字体大小写的方法
- Android preference 自定义控件
- Android 自定义 Preference
- android preference自定义
- Android 自定义preference组件
- Android PreferenceScreen
- Android中preference 自定义样式
- android 自定义preference的属性
- Android如何自定义Preference呢?
- android 自定义Preference(APIDemons中自定义Preference解析)
- Android 移除Prefrence & PreferenceCategory
- MFC下的各种字符串类型和相互转换
- 程序在内存中的分布
- TLB (Translation Lookaside Buffer)简介
- JNDI数据源配置
- Data Guard Service 相关介绍
- android 4.2 自定义PreferenceCategory PreferenceScreen Preference等
- Android设备管理器——DevicePolicyManager
- 一般函数指针和类的成员函数指针
- Linux下定时执行脚本
- 反射调用成员变量
- IncrediBuild工具使用及设置
- Linux Shell脚本编写
- 控制Android应用程序的耗电量
- tomcat加载项目原理解惑