7

我想将我的 Android 应用首选项屏幕的外观更改为深色文本颜色。我怎样才能做到这一点?(我已经将背景更改为白色)

4

2 回答 2

15

我假设您使用扩展PreferenceActivity. 您可以使用该setTheme方法在您的首选项屏幕上设置自定义主题。只需在res/values/themes.xml.

它看起来像这样:

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

然后在您的活动中设置它:

setTheme(R.style.Theme_DarkText);
于 2011-04-07T11:20:57.893 回答
0

我接受了乌迪尼克的想法,但我改进了一点。现在可以随时设置(在这种情况下)PreferenceCategory 的颜色,而不仅仅是在膨胀视图时。

怎么做 ?

首先,创建您的自定义类,例如:

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);
}

}

完成后,您必须在使用 XML 定义设置时使用它。

在你只需要在你的 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);
        }
    }

这是想法。您可以随时为您的任何设置执行此操作。使用此代码之前必须完全加载布局,否则getListView().getCount() 将返回0。例如,如果您在onCreate 中使用它,我将无法工作。如果你想在启动时这样做,我建议你在 onWindowFocusChanged 方法中这样做。

于 2012-12-19T14:24:46.757 回答