【问题标题】:Why is the text black instead of white when I create a navigation dropdown in an ActionBar?当我在 ActionBar 中创建导航下拉菜单时,为什么文本是黑色而不是白色?
【发布时间】:2015-01-22 00:44:15
【问题描述】:

注意:这是我的第一个 Android 应用程序,我不知道自己在做什么。在不解释我应该寻找什么的情况下将我引导至文档将无济于事,因为我已经尝试阅读文档并且没有理解它。

我正在 ActionBar 中创建导航下拉菜单,文本是黑色而不是白色(就像我的 ActionBar 中的其余文本一样)。我认为这是因为我在 ArrayAdapter 中使用了错误的东西,但其他值都没有更好的效果。下面是我用来创建下拉菜单的代码。

//NOTE: folders will be created at runtime from data fetched from a web service,
//these values are just test data to get the feature working
folders = new ArrayList<String>(Arrays.asList("All Folders", "comics"));
final ArrayAdapter<String> aa = new ArrayAdapter<String>(
    this,
    android.R.layout.simple_list_item_1,
    folders
);

final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
bar.setListNavigationCallbacks(aa, new ActionBar.OnNavigationListener() {
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        final Context c = getApplicationContext();
        Toast.makeText(c, folders.get(itemPosition), Toast.LENGTH_SHORT).show();
        return true;
    }
});

我所看到的示例:

我想了解为什么文本颜色不对,并希望如何用正确的颜色创建它。我不想自定义颜色(就像我发现的很多问题一样),我只想以与 ActionBar 中其他内容相同的样式创建它。

这个styles.xml文件:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

【问题讨论】:

  • simple_list_item_1 的源代码在设置文本颜色方面没有任何作用,因此它会尝试遵循应用程序的主题是有道理的。我之前错误地做的是创建视图并在每个视图中更改主题,后来意识到如果我将应用程序主题更改为我希望在整个应用程序中保持一致的内容,那么一切都会按预期运行,而无需以编程方式更改任何内容。有没有可能是您的主题化导致了这个问题?
  • 能否请您发布您正在使用的主题?您可以在styles.xml 文件中找到它。

标签: android android-actionbar android-spinner


【解决方案1】:

正如我所怀疑的那样,人们所建议的所有样式都只是在修补那些一开始就做错事的代码。 onCreate 中的代码应该是这样的:

    // Set up the dropdown list navigation in the action bar.
    actionBar.setListNavigationCallbacks(
            // Specify a SpinnerAdapter to populate the dropdown list.
            new ArrayAdapter<String>(
                    actionBar.getThemedContext(),
                    android.R.layout.simple_list_item_1,
                    android.R.id.text1,
                    new String[] {
                            "All Folders",
                            "comics",
                            "test"
                    }),
            this);

我应该有一个onNavigationItemSelected 方法:

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // When the given dropdown item is selected, show its contents in the
    // container view.

    return true;
}

其中的关键部分是actionBar.getThemedContext() 方法。

【讨论】:

    【解决方案2】:

    添加到Chas。如果您使用 Jellybean 及以上版本,Owens 回复 actionBar.getThemedContext() 效果很好。

    对于其他人 ArrayAdapter.createFromResource(getApplicationContext(), ...) 也同样有效

    【讨论】:

      【解决方案3】:

      您需要为该活动/应用程序选择一种样式。参考此example 将导航项设为白色。

      【讨论】:

      • 我看到了,它对我不起作用。要么我做错了,那个答案解释得不够好,要么它不适用于错误的地方。它也没有回答我的问题,即“为什么文本是黑色而不是白色......”。我这样问这个问题是因为我想了解它为什么坏了。此外,我不敢相信 Android 已经彻底崩溃,以至于你必须跳上铁圈才能做这么简单的事情。他们让添加导航下拉菜单变得如此简单,以至于我无法相信您必须实现自己的样式才能使其正常工作。
      • 理想情况下设置样式应该有效。在 OnnavigationItemSelected 试试这个; ((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);这会将 textview 的 textcolor 显式设置为白色。
      【解决方案4】:

      bar.getThemedContext() 技巧对我有用。和你一样,我徘徊了很久,为什么文字是黑色而不是白色。我自己不会发现的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多