Android preferences summary default color?

前端 未结 6 928
梦谈多话
梦谈多话 2021-01-03 02:30

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

相关标签:
6条回答
  • 2021-01-03 02:44
    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

    0 讨论(0)
  • 2021-01-03 02:54

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

    0 讨论(0)
  • 2021-01-03 02:58

    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.

    0 讨论(0)
  • 2021-01-03 02:59

    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.

    0 讨论(0)
  • 2021-01-03 03:01

    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...

    0 讨论(0)
  • 2021-01-03 03:05

    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!

    0 讨论(0)
提交回复
热议问题