android自定义ListPreference字体大小

来源:互联网 发布:小说小偷源码 编辑:程序博客网 时间:2024/06/05 15:15

这篇博客算是自己的一个记录吧,组长给了一个任务,需要实现一个紧急广播的一系列设置功能,但是客户那边设置界面的字体大小有特殊的要求,看了一下,对于ListPreference这样的控件,android系统没有提供直接的属性来设置,只好自己重写一个了.

自定义属性

<?xml version="1.0" encoding="utf-8"?><resources>    <attr name="titleTextSize" format="dimension" />    <attr name="summaryTextSize" format="dimension" />    <declare-styleable name="CustomTitleView">        <attr name="titleTextSize" />        <attr name="summaryTextSize" />    </declare-styleable></resources>

这里,我定义了ListPreference中title和summary的字体大小的属性,很简单,这里就不在多说了。

编写类继承自ListPreference

import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.os.Parcel;import android.os.Parcelable;import android.preference.ListPreference;import android.preference.Preference.BaseSavedState;import android.text.TextUtils;import android.util.AttributeSet;import android.util.TypedValue;import android.view.View;import android.widget.TextView;@SuppressLint("NewApi") public class MyListPreference extends ListPreference{    private int mTitleTextSize;    private int mSummaryTextSize;    private String mValue;    private String mSummary;    private int mDefaultValue;    public MyListPreference(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    @SuppressLint("NewApi")     public MyListPreference(Context context, AttributeSet attrs,            int defStyleAttr) {        this(context, attrs, defStyleAttr,0);    }    public MyListPreference(Context context, AttributeSet attrs,            int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);        TypedArray arrayPreference = context.obtainStyledAttributes(attrs,                com.android.internal.R.styleable.Preference, defStyleAttr,                defStyleRes);        // 获取默认的值        mDefaultValue = arrayPreference.getInt(com.android.internal.R.styleable.Preference_defaultValue, 1);        arrayPreference.recycle();        TypedArray appearance = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyleAttr, 0);        // 获取所有自定义属性的值        for (int i = appearance.getIndexCount(); i >= 0; i--) {            int attr = appearance.getIndex(i);             switch (attr) {                case R.styleable.CustomTitleView_titleTextSize:                    mTitleTextSize = px2dip(context,appearance.getDimensionPixelSize(attr,13));                    break;                case R.styleable.CustomTitleView_summaryTextSize:                    mSummaryTextSize = px2dip(context,appearance.getDimensionPixelSize(attr,11));                    break;            }        }        appearance.recycle();    }    @Override    protected void onBindView(View view) {        final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);         if (titleView != null) {             titleView.setTextSize(mTitleTextSize);                final CharSequence title = getTitle();                if (!TextUtils.isEmpty(title)) {                    titleView.setText(title);                    titleView.setVisibility(View.VISIBLE);                } else {                    titleView.setVisibility(View.GONE);                }            }         final TextView summaryView = (TextView) view.findViewById(                    com.android.internal.R.id.summary);             summaryView.setTextSize(mSummaryTextSize);            if (summaryView != null) {                final CharSequence summary = getSummary();                if (!TextUtils.isEmpty(summary)) {                    summaryView.setText(summary);                    summaryView.setVisibility(View.VISIBLE);                } else {                    summaryView.setVisibility(View.GONE);                }            }    }    @Override    public CharSequence getSummary() {        final CharSequence entry = getEntry();        return entry;    }    @Override    public void setSummary(CharSequence summary) {        super.setSummary(summary);        mSummary = (String) summary;    }    public int px2dip(Context context, float pxValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (pxValue / scale + 0.5f);    }}

上面的代码是,在构造方法里获取自定义的属性,然后在重写的onBindView方法中设置其title和summary的文字大小和颜色。

布局使用

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"              xmlns:custom="http://schemas.android.com/apk/res/com.test.settings"              android:key="cell_broadcast_settings">    .....    <com.test.settings.MyListPreference        android:defaultValue="2"        android:dialogTitle="@string/alert_reminder"        android:entries="@array/alert_sound_duration_entries"        android:entryValues="@array/alert_sound_duration_values"        android:key="alert_reminder_type"        android:title="@string/alert_reminder"        custom:titleTextSize="13sp"        custom:summaryTextSize="11sp"        /></PreferenceScreen>

此时效果如下:
这里写图片描述

以上算是自己的一个记录吧,可以看到虽然实现了这样的效果,但是由于在MyListPreference中使用了系统的资源,比如”com.android.internal.R.styleable.Preference”,所以必须在源码下编译才可以,大家如果有更高的处理方法,也欢迎一起讨论。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在火车上肚子疼怎么办 户口是学签证怎么办 学生去韩国护照怎么办 户口迁移原护照怎么办 考科四档案丢了怎么办 驾照档案袋丢了怎么办 驾照考试档案丢失怎么办 驾校不给档案怎么办 学员卡丢了怎么办 第一次办身份证急用怎么办 医保卡没有磁性怎么办 退休后大病医保怎么办 退休了医保不够怎么办 社保采集信息后怎么办 医保卡个人欠费怎么办 离婚房产不过户怎么办 科目四五次不过怎么办 科二考试下雨怎么办 科目三无法超车怎么办 下雨天考科目二怎么办 考科二下雨对点怎么办 上海自考准考证过期怎么办 驾照到年龄后怎么办 科三考试约不上怎么办 科目四预约不了怎么办 科目三考试迟到怎么办 科目四无法预约怎么办 新车换年检标志怎么办 科目一二次不过怎么办 科目一两次不过怎么办 考科目一没考过怎么办 科目一挂了怎么办 科目一考试不及格怎么办 科二考试不合格怎么办 驾校没给档案怎么办 考科目一没带身份证怎么办 企业经营期限届满怎么办 网上买票待核验怎么办 手机购票待审核怎么办 护士注册过期了怎么办 驾照已经过期了怎么办