android 4.2 自定义PreferenceCategory PreferenceScreen Preference等

本文介绍了如何在Android 4.2中针对PreferenceCategory进行样式定制,包括更改title的颜色和横线颜色。通过创建自定义布局文件并在代码中设置颜色值,或者在XML中直接设置,可以实现需求。示例中提到了自定义ContactsPreferenceCategory,并展示了在包名com.android.contacts下的属性引用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:

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 对比(其实没改变什么, 就是把颜色改了而已):




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值