Replace the action bar's title with a spinner (drop down)

荒凉一梦 提交于 2019-12-12 08:29:38

问题


I am trying to display a spinner in the same position where the action bar's default title appear. I followed the instruction of the similar SO case here , so I managed to eliminate the title but still the spinner's position is not aligned to the left, as you can see from this screen-shot

Here are the main definitions of my application to reproduce this case:

AndroidMenifest.xml:

<application
        android:label="app"            
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.AppCompat" > 
...
    <activity
        android:name="gm.activities.ViewAllActivity">            
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="gm.activities.MainActivity" />
    </activity>

menu_view_all.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="gm.activities.ViewAllActivity">
    <item android:id="@+id/spinner"
        android:title="will be replaced anyway"
        app:showAsAction="ifRoom"
        app:actionViewClass="android.widget.Spinner"
        android:layout_gravity="left"
        android:gravity="left"/>
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

and the relevant activity:

public class ViewAllActivity extends ActionBarActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_all_activity);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
...
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_view_all, menu);
        MenuItem item = menu.findItem(R.id.spinner);
        Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
        spinner.setGravity(Gravity.LEFT);
        SpinnerAdapter adapter;
        spinner.setAdapter(ArrayAdapter.createFromResource(this,
                R.array.all_table_views, android.R.layout.simple_spinner_item));
        spinner.setOnItemSelectedListener(this); // set the listener, to perform actions based on item selection
        return true;
     }

So - Can I align the spinner to the left of the action bar and how? Is it correct to use spinner inside the actionbar and to set it through the menu.xml file as I did?


回答1:


Action Views in the menu are always going to align to the right. If you want your Spinner to align left, it would be better to set it as a custom View on the ActionBar using the setCustomView() method. A custom View will align to the left by default, and will take the place of the title if that is hidden. Please note that this requires that you call setDisplayShowCustomEnabled(true) on the ActionBar.



来源:https://stackoverflow.com/questions/28658287/replace-the-action-bars-title-with-a-spinner-drop-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!