Cha*_*ens 1 android android-spinner android-actionbar
注意:这是我的第一个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;
}
});
Run Code Online (Sandbox Code Playgroud)
我所看到的例子:

我想了解为什么文本是错误的颜色,并希望如何使用正确的颜色创建它.我不想自定义颜色(就像我发现的很多问题一样),我只是希望它以与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>
Run Code Online (Sandbox Code Playgroud)
正如我所怀疑的那样,人们所建议的样式的所有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);
Run Code Online (Sandbox Code Playgroud)
我应该有一个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;
}
Run Code Online (Sandbox Code Playgroud)
关键部分是actionBar.getThemedContext()方法.