Android CheckBoxPreference title color

試著忘記壹切 提交于 2019-12-25 01:19:05

问题


I am using the code in this answer.

But instead of setting the android:textColor for the Text View i use

style="?background_text_color_theme"

which has to set the text color depending on the theme of the application ( white text color for black theme and vice versa). This works in all other places except for CheckBoxPreference.

Changes I made to the link I gave:

      <TextView android:id="@+android:id/title"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:singleLine="true" 
        android:ellipsize="marquee" android:fadingEdge="horizontal"
        style="?background_text_color_theme" />

Here is my style.xml:

    <style name="background_text_color_theme_bl">
    <item name="android:textColor">#ffffff</item>

</style>    

     <style name="background_text_color_theme_wh">
    <item name="android:textColor">#000000</item>

</style>    

Themes.Xml:

    <style name="Theme.White"  parent="@android:style/Theme.Holo.Light">
    <item name="background_text_color_theme">@style/background_text_color_theme_wh</item>
</style> 

   <style name="Theme.Black"  parent="@android:style/Theme.Holo">
    <item name="background_text_color_theme">@style/background_text_color_theme_bl</item>
</style>

But the text color does not seem to be set according to the style. Can anyone tell me why is it happening?

Thank You.


回答1:


Here is how I did it..Just in case some one needs it in the future.

    public class CheckBoxPreferenceClass extends CheckBoxPreference {


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

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

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView title = (TextView) view.findViewById(android.R.id.title);
        title.setTextColor(Color.BLACK);
    }

}

And in the preference xml use this for the checkbox preference:

  <packagename.CheckBoxPreferenceClass
        android:key="@string/imp"
        android:title="@string/title"/>


来源:https://stackoverflow.com/questions/12004326/android-checkboxpreference-title-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!