1)我对Spinner
有一些奇怪的行为。如果我使用Action Bar
将其添加到ActionBar.NAVIGATION_MODE_LIST
。
ArrayAdapter<CharSequence> barAdapter = new ArrayAdapter<CharSequence>(mContext, R.layout.projects_filter_item, android.R.id.text1, mContext.getResources().getStringArray(R.array.projects_filteres));
barAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
getActionBar().setListNavigationCallbacks(barAdapter, mOnNavigationListener);
getActionBar().setDisplayShowTitleEnabled(false);
然后它像这样黑暗:
2)但是,如果我在onCreateOptionsMenu
中添加ActionView
,则此箭头为白色。我不会改变任何bg或颜色属性。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.action_bar_context_menu, menu);
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mFilterSpinner = (Spinner)MenuItemCompat.getActionView( filter);
mFilterSpinner.setAdapter(adapter);
mFilterSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long pos) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
3)如果我在片段中的某处添加Spinner,它看起来很好(很暗)。
为什么Spinner
有这种不同的行为?
答案 0 :(得分:0)
主题应该是:
<style name="MyTheme" parent="SomeParentStyle>
<item name="actionDropDownStyle">@style/MyActionBarSpinnerStyle</item><!-- if you use ABS -->
<item name="android:actionDropDownStyle">@style/MyActionBarSpinnerStyle</item>
</style>
MyActionBarSpinnerStyle
应该是这样的:
<style name="MyActionBarSpinnerStyle" parent="@style/MyDropDownActionBarStyle">
<item name="android:background">@drawable/my_custom_state_list_drawable</item>
</style>
如果您使用ABS,则MyDropDownActionBarStyle
代替Widget.Sherlock.Light.Spinner.DropDown.ActionBar
,如果仅定位原生ActionBar,则应该Widget.Holo.Light.Spinner.DropDown.ActionBar
。
答案 1 :(得分:0)
改变主题并没有帮助。如果我以编程方式添加Spinner
,请执行以下操作:
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
mFilterSpinner = new Spinner(getActionBar().getThemedContext()); // doesn't matter what context is here, Activity, Application or ActionBar
filter.setActionView(mFilterSpinner);
Spinner
的箭头是白色的。
但正如我所说,如果我在xml中添加Spinner它看起来很好。
所以,我只是创建菜单项为LinearLayout
<item
android:title="Filter"
android:visible="false"
android:id="@+id/context_menu_filter"
app:actionViewClass="android.widget.LinearLayout"
android:orderInCategory="1"
app:showAsAction="always"
/>
使用Spinner
创建简单的xml:
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
并在onCreateOptionsMenu
手册中对此布局进行了充气并添加到LinearLayout:
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
mFilterSpinner = (Spinner) ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.temp_spinner, null);
filter.setVisible(true);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
LinearLayout l = (LinearLayout) MenuItemCompat.getActionView( filter);
l.addView(mFilterSpinner);
mFilterSpinner.setAdapter(adapter);
现在它应该是黑暗的。