简体   繁体   中英

Action bar (Sherlock) menu items cutting off built-in Spinner

My action bar menu items are cutting off the width of the action bar Spinner so that the Spinner's text gets truncated. I've set all my menu items to use "ifRoom" but unfortunately they interpret the original Spinner width as available room.

Here's the truncation when I have three menu items (yes, I know the third icon looks identical to the second):

带截头微调器的操作栏

Here's what I get when I comment out the third menu item. Ultimately, I want something that looks like this, but with an overflow icon showing instead of the search icon:

带完整微调器的操作栏,但只有两个菜单项

I populate the Spinner using a custom adapter (extending from BaseAdapter and implementing SpinnerAdapter).

Here's my menu XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/ab_refresh"
        android:orderInCategory="1"
        android:showAsAction="ifRoom"
        android:title="@string/ab_refresh"
        android:icon="@drawable/navigation_refresh"/>

    <item
        android:id="@+id/ab_search"
        android:orderInCategory="2"
        android:showAsAction="ifRoom|collapseActionView"
        android:title="@string/ab_search"
        android:icon="@drawable/action_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>

    <item
        android:id="@+id/ab_toggle"
        android:orderInCategory="3"
        android:showAsAction="ifRoom"
        android:title="@string/ab_latest"
        android:icon="@drawable/action_search"/>

</menu>

And here's my ab_dropdown XML (used in getView of my custom adapter):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rel"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:gravity="fill_horizontal">

    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="1dp" />

</RelativeLayout>

What I've tried:

  1. Setting a minWidth of 570dp for the RelativeLayout and TextView of ab_dropdown. No effect regardless of whether I set it in XML or the getView method of my custom adapter.
  2. Hardcoding the layout_width of RelativeLayout and TextView to 570dp. Again, no effect in XML or code.
  3. I WOULD attempt to find the action bar Spinner programmatically so as to set its width, but when I tried to do this for a different reason , I couldn't.

Any ideas?

Personally, I'd switch to a navigation drawer and dump the drop-down list navigation.

That being said, it sounds like you need two versions of your menu resource. Have the third item be never in the toolbar (always in overflow) by default, and have it be ifRoom on larger screens (eg, res/menu-sw600dp/ ).

You could also experiment with AutoScaleTextView for your drop-down list navigation labels.

The navigation portion of the action bar is deemed least important, which is why ifRoom considers there to be room.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM