Preference-自定義PreferenceCategory

1. Preference-自定義PreferenceCategory

修改字體大小、樣式、顏色

2. 參考代碼

package com.android.fadi.powersave.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.fadi.powersave.util.ScreenUtils;

public class DamiPreferenceCategory extends PreferenceCategory {

    public DamiPreferenceCategory(Context context) {
        super(context);
    }

    public DamiPreferenceCategory(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DamiPreferenceCategory(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtils.dpToPxInt(getContext(), 32)));
        if (view instanceof TextView) {
            TextView tv = (TextView) view;
            tv.setBackgroundColor(Color.parseColor("#fafafa"));
            tv.setTextColor(Color.parseColor("#009688"));// 字體顏色
            tv.setSingleLine(true);
            tv.setPadding(tv.getPaddingLeft(), 0, tv.getPaddingLeft(), 0);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL));// 字體風格
            tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章