简体   繁体   English

Android ActionBar项目最大宽度

[英]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. 我目前正在尝试在菜单上显示3件事:该应用程序的图标,微调器和一个按钮。 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). 在第一张图片中,您可以看到右侧的按钮(类似邮件的图标),当我手动将微调框的宽度设置为60dp时显示该按钮(这不是我想要的,因为该应用程序可能用于平板电脑和手机) 。 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 这是我的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" 困扰我的是,我无法将.xml更改为RelativeLayout,就像在许多教程中所看到的那样,例如: Android Developers MyFirstApp-“菜单无法解析或不是字段”但是每个时间我尝试“删除xml”

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

is autogenerated by eclipse (forcing me to have a "menu"). 由eclipse自动生成(强制我创建一个“菜单”)。 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 让我知道是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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