当我在ActionBar中创建导航下拉列表时,为什么文本是黑色而不是白色?

时间:2013-09-06 02:57:35

标签: 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>

4 个答案:

答案 0 :(得分:5)

正如我所怀疑的那样,人们所建议的所有样式的jiggery-pokery都只是修补了首先做错事的代码。 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()方法。

答案 1 :(得分:3)

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

对于其他人,ArrayAdapter.createFromResource(getApplicationContext(),...)也可以正常工作

答案 2 :(得分:1)

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

答案 3 :(得分:-2)

bar.getThemedContext()技巧对我有用。就像你一样,我长时间徘徊为什么文字是黑色而不是白色。我不会发现自己。