Android preferences summary default color?

2019-02-10 05:01发布

I have installed my app in a real phone, and even though in the emulator all the texts of the preferences summaries seem to be in the same color, in the real phone the color is different (some kind of blue... but I guess it depends on the phone's model).

How can I set this color to my custom preference component? (I have implemented my own seek bar, and its summary text color is different from all the other components text color...).

Thanks!

6条回答
地球回转人心会变
2楼-- · 2019-02-10 05:11
Preference pUpdate = findPreference("sys_setting_update");
pUpdate.setSummary(Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>"));

use Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>") to setSummary

查看更多
女痞
3楼-- · 2019-02-10 05:15

I have figured out a way to retrieve the default color used by the Android device your application is running in. It is a bit tricky and requieres that you retrieve the color being shown from another Preference Summary View of your activity and store it in runtime.

Then you can use the same color code in other Views of other preferences, assuring that you will allways get the same color code Android assigned to the standard preferences. Here is how I did it:

My preferences activity has a normal CheckBoxPreference that I use to activate or deactivate a service. I have extended CheckBoxPreference as follows, so my extension retrieves in rutime the default color Android finally gave to the summary of that CheckBoxPreference:

public class MyCheckBoxPreference extends android.preference.CheckBoxPreference {

    private static int sSummaryColor = Color.WHITE;
    private static boolean sInitialized = false;

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

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

    public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override    
    public void onBindView(View view) {
        super.onBindView(view);
        if (!sInitialized) {
            sSummaryColor = getSummaryColor(view);
            sInitialized = true;
        }
    }

    private int getSummaryColor(View view) {

       int color = Color.WHITE;

        // Gets the color android gave to the summary by default
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 
        if (summaryView != null) {
           ColorStateList list = summaryView.getTextColors();
            if (list != null) {
                color = list.getDefaultColor();
            }
        }
        return color;
    }

    public static int getSummaryColor() {
        return sSummaryColor;
    }
}

In my preferences.xml I instantiate that preference as MyCheckBoxPreference instead of just CheckBoxPreference:

<org.yourpackage.MyCheckBoxPreference
                android:title="@string/preference_title_activate" 
                android:defaultValue="false" 
                android:summary="@string/preference_summary_activate_off" 
                android:summaryOff="@string/preference_summary_activate_off"
                android:key="preference_activate">
</org.yourpackage.MyCheckBoxPreference>

The MyCheckBoxPreference has to be instantiated once before retrieving the summary color with MyCheckBoxPreference.getSummaryColor().

Now you can set the color of other customized preferences from onBindView(View):

public class MyCustmizedPreference extends Preference {
    public MyCustmizedPreference (Context context) {
        super(context);
        setLayoutResource(R.layout.my_customized_preference);
    }

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

        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
        if (summaryView != null) {
            summaryView.setTextColor(MyCheckBoxPreference.getSummaryColor());
        }        
    }
}

It actually works under Samsung Galaxy S. I have also tested that it doesn't break anything under the emulator.

查看更多
乱世女痞
4楼-- · 2019-02-10 05:18

I don't think this is possible. I am able to change the background color and the title text color, but not the summary color.

Background:

getListView().setBackgroundColor(Color.TRANSPARENT);

Title text:

Preference yourpreference = findPreference("yourpreference");
TextView tv = (TextView)yourpreference.getView(null, getListView());
tv.setTextColor(...);

Sorry I couldn't help more...

查看更多
该账号已被封号
5楼-- · 2019-02-10 05:21

I found these: android:textAppearance="?android:attr/textAppearanceLarge" and android:textAppearance="?android:attr/textAppearanceSmall" seem to do the trick.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-02-10 05:30

The Samsung Galaxy S phones have their own Preference layout with the text color specified for the Summary line. Even though a TextAppearance.Small is specified the textColor attribute of the layout is overriding the text appearance.

查看更多
Bombasti
7楼-- · 2019-02-10 05:33

I had the same problem and I've been experimenting with my custom seekbar-preference's style. Finally these lines in onCreateView method of seekBarPreference.java show preference's summary with default text color:

TextView summaryText = new TextView(getContext());
summaryText.setText(getSummary());
summaryText.setTextAppearance(getContext(), android.R.style.TextAppearance_Small);

I use it on preference_screen.xml:

<com.asdasf.SeekBarPreferencias
        android:key="@string/pref_seekBar_distance_key"
        android:id="@+id/mySeekBarPreference"
        android:title="@string/pref_seekBar_distance_title"
        android:summary="@string/pref_seekBar_distance_summary"        
        android:max="50"
        android:defaultValue="12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

I hope it will be useful...(and that I have written well my first answer) Regard!

查看更多
登录 后发表回答