android - 为什么在 ActionBar 中创建导航下拉菜单时文本是黑色而不是白色?

标签 android android-actionbar android-spinner

注意:这是我的第一个 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;
    }
});

我看到的示例:

black text on gray background

我想了解为什么文本的颜色不对,并希望了解如何使用正确的颜色来创建它。我不想自定义颜色(就像我发现的很多问题一样),我只想以与 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>

最佳答案

正如我所怀疑的那样,人们建议的所有带有样式的拼图游戏都只是修补一开始就做错事的代码。 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() 方法。

关于android - 为什么在 ActionBar 中创建导航下拉菜单时文本是黑色而不是白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649159/

相关文章:

android - 为什么当我的 Android 应用程序在我的服务器上调用我的 API 时,我得到一个空响应?

android - 为什么我的微调器看起来不像微调器?

android - 进入 Activity 时自动选择微调项。我该如何避免这种情况?

android - 在 Android 和 Raspberry Pi 3 Linux 之间设置 Wifi-Direct 连接

android - 处理 fragment 菜单选项

android - 蜂窝中的附加数据库

android - 检测操作栏图标长按

java - 如何为每个不同的 fragment 设置操作栏

java - 访问 ActionBar 时出现 nullPointerException

java - 如何使用 Intent 打开电子邮件应用程序