我目前正在为ActionBar(ActionBarCompat)工作,我需要一些关于actionbar的澄清.
我正在使用像这样的微调器布局,
<Spinner android:id="@+id/SpinnerList" android:layout_width="fill_parent" android:layout_height="wrap_content" />
我的代码,
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{ private boolean mAlternateTitle = false; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); Spinner spinner = (Spinner) findViewById(R.id.SpinnerList); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.locations,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } }
它在我的android galaxy ace中显示正常的微调器.我需要输出如下所示,
是那个位置android 2.3.请给出建议.
谢谢.
解决方法
试试这个代码.
在menu / main.xml中:
在menu / main.xml中:
<item android:id="@+id/menu_spinner1" android:showAsAction="always" android:orderInCategory="1" android:actionViewClass="android.widget.Spinner" >
在活动中:
private MenuItem mSpinnerItem1 = null; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. MenuInflater mi=getMenuInflater(); mi.inflate(R.menu.main,menu); mSpinnerItem1 = menu.findItem( R.id.menu_spinner1); View view1 = mSpinnerItem1.getActionView(); if (view1 instanceof Spinner) { final Spinner spinner = (Spinner) view1; spinner.setAdapter(ad1); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } return true; }