简体   繁体   中英

Android ActionBar Item max width

I'm currently trying to show 3 things on my menu: The app's icon, a spinner, and a button. The problem is, that the spinne takes too much space, hiding the button (which is very important for the users).

In the first picture you can see the button on the right (mail-like icon), which was shown when I manually set the spinner's width to 60dp (which is not what I want, as the app might be used for tablets and phones). As you can see, in the second image the spinner is taking all the space, hiding the button.

显示按钮

按钮被隐藏

Here's the code of my activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.androidinit.ActivityMain" >
    <item
        android:id="@+id/institution_selector"
        android:showAsAction="always|withText"
        android:title="@string/empty_string"/>
    <item
        android:id="@+id/institution_spinner"
        android:showAsAction="always|withText"
        android:title="@string/no_institution"
        android:actionLayout="@layout/ppn_institution_spinner_menu"  />

    <item
        android:id="@+id/send_message_teacher_main_button"
        android:orderInCategory="100"
        android:showAsAction="always|withText"
        android:icon="@drawable/icon_message"
        android:title="@string/send_message_teacher_main"/>

What I would like to do is to set a "weight" for the spinner, which should be exactly the width of the screen - icon - button, however I haven't been able to do this. I've followed lots of tutorials, but I just can't get it to work.

Something that has been bothering me is that I can't change the .xml to a RelativeLayout, as it is seen on many tutorials, for example this one: Android Developers MyFirstApp - "menu cannot be resolved or is not a field" However each time I try "deleting the xml"

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

is autogenerated by eclipse (forcing me to have a "menu"). I hope I'm missing something, because I have been going through this for hours, but still nothing.

lets do this.. mostly its taking all the space because of the length of your text or items so to hack it.. do this

 yourspinner.setGravity(Gravity.START);

or

 yourspinner.setScaleX((float)0.2); //choose any preferred decimal

or

 LayoutParams layoutParams = (LinearLayout.LayoutParams) yourspinner.getLayoutParams();
 layoutParams.width = (Yourspinner. getWidth()%2);
 yourspinner.setLayoutParams(layoutParams);

the last one might be promising..

maybe they might help shrink the spinner in the actionbar

NOTE: this is a wild guess about functionality..

let me know if it helps

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