Change PreferenceActivity text color

喜你入骨 提交于 2019-12-18 13:37:12

问题


I want to change the look of my Android app's preference screen to dark text color. How can I do this? (I´ve already changed the background to white color)


回答1:


I assume you use an Activity which extends the PreferenceActivity. You can use the setTheme method to set a custom theme on your preference screen. Just define one in res/values/themes.xml.

It would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="Theme.DarkText">
    <item name="android:textColor">#000000</item>
  </style>
</resources> 

Afterwards set it in your Activity:

setTheme(R.style.Theme_DarkText);



回答2:


I took the idea of Udinic, but I improved it a little. It is now possible to set the color of (in this case) the PreferenceCategory at any time, and not only when inflating the view.

How To do it ?

First, create your customized class such as this one :

import android.content.Context;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyPreferenceCategory extends PreferenceCategory {

private TextView categoryTitle;

public PincardPreferenceCategory(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public PincardPreferenceCategory(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public PincardPreferenceCategory(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}


@Override
protected View onCreateView(ViewGroup parent) {
    categoryTitle =  (TextView)super.onCreateView(parent);
    return categoryTitle;
}


public void setBackgroundColor(int color) {
    categoryTitle.setBackgroundColor(color);
}


public void setTextColor(int color) {
    categoryTitle.setTextColor(color);
}

}

Once it is done, you have to use it while defining your settings in XML.

After you just have to use this loop in your java preferenceActivity :

    for (int i = 0; i < getListView().getCount(); i++) {
        Object view = getListView().getItemAtPosition(i);
        if (view instanceof PincardPreferenceCategory) {
            ((PincardPreferenceCategory)view).setBackgroundColor(Color.BLUE);
            ((PincardPreferenceCategory)view).setTextColor(Color.RED);
        }
    }

Here is the idea. You can do that for any of your settings, and at anytime. The layout must have been totally loaded before using this code, because otherwise, getListView().getCount() will return 0. I won't work if you use it in onCreate for exemple. If you want to do that at startup, I suggest you to do that in the onWindowFocusChanged method.



来源:https://stackoverflow.com/questions/5571603/change-preferenceactivity-text-color

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