更换操作栏的冠军,微调(下拉)冠军、操作

2023-09-11 07:49:50 作者:桀骜不驯

我想显示在操作栏中的默认标题出现在同一个位置上的微调。 我遵循了类似的指令SO在此情况下, 所以我设法消除冠军,但仍是微调的位置不是左对齐,因为你可以从这个屏幕截图看

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

下面是我的应用程序,以重现这种情况的主要定义: AndroidMenifest.xml:

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>

及相关活动:

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;
     }

所以 - 我可以调整微调的操作栏,以及如何左边? 它正确使用微调的动作条内部通过的 menu.xml文件的设置它的文件像我一样?

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?

推荐答案

动作查看 S在菜单中总是会右对齐。如果你希望你的微调来左对齐,倒不如将其设置为在动作条使用 setCustomView()方法。自定义查看将调整到左边默认,并会拿冠军的地方,如果这是隐藏的。请注意,这需要你调用 setDisplayShowCustomEnabled(真)动作条

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.