1

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

4 回答 4

5

正如我所怀疑的那样,人们建议的所有风格的乱七八糟的游戏都只是修补最初做错事的代码。中的代码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()方法。

于 2013-09-09T13:20:33.940 回答
3

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

对于其他人 ArrayAdapter.createFromResource(getApplicationContext(), ...) 也可以

于 2013-12-16T09:14:18.923 回答
1

您需要为该活动/应用程序选择一种样式。请参阅此示例以使导航项变为白色。

于 2013-09-06T03:19:51.943 回答
-2

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

于 2013-11-24T09:36:29.797 回答