gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:38:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com