简体   繁体   中英

ListPreference change dialog style (radio button color)

ListPreferenceHey I'm using PreferenceActivity and added radio button to it using ListPreference. The problem is that listPreference uses it own dialog which has blue radio button (green on lollipop) and I need to change it to orange. I managed to get the dialog and change the headline and the divider color using the following:

listPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            AlertDialog dialog = (AlertDialog) listPreference.getDialog();
            if (dialog != null) {
                changeDialog(getApplicationContext(), dialog);
            }
            return true;
        }}); 



public void changeDialog(Context context, final AlertDialog dialog) {
        int titleViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
        TextView title = (TextView) dialog.findViewById(titleViewId);
        title.setTextColor(context.getResources().getColor(R.color.orange));
    }

So if the title id is "android:id/alertTitle" and the divder id is "android:id/titleDivider", what is the id for the radio button?

The solution for me was to change the color in the colors.xml file.

So, at first I had this in my colors.xml :

<!-- skyblue color -->
<color name="teal_200">#FF03DAC5</color>

And the color of ListPreferense radio buttons was skyblue. And I got the idea to change the color in colors.xml to green, like this:

<!-- green color -->
<color name="teal_200">#1ED760</color>

And it worked! The radio buttons color of ListPreferense became green.

正确的做法是在主题中使用<item name="colorAccent">YOUR COLOR</item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM