简体   繁体   English

Android:如何在操作栏中添加两个按钮

[英]Android: How do I add two buttons into the action bar

I would like to add two buttons into the action bar, but in my case just one button appears. 我想在操作栏中添加两个按钮,但就我而言,只有一个按钮出现。 What is wrong with my code? 我的代码有什么问题?

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myApp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_action_settings"
      myApp:showAsAction="ifRoom"/>

<item android:id="@+id/action_about"
    android:title="@string/action_about"
    android:icon="@drawable/ic_action_about"
    myApp:showAsAction="ifRoom"/> 
</menu>

like this use always instead of ifRoom 像这样always使用而不是ifRoom

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_action_settings"
      myapp:showAsAction="always"/>

<item android:id="@+id/action_about"
    android:title="@string/action_about"
    android:icon="@drawable/ic_action_about"
    myapp:showAsAction="always"/> 
</menu>

Read documentation 阅读文件

You can also use "always" to declare that an item always appear as an action button. 您也可以使用“始终”声明某项始终作为操作按钮出现。 However, you should not force an item to appear in the action bar this way. 但是,您不应该以此方式强制项目显示在操作栏中。 Doing so can create layout problems on devices with a narrow screen. 这样做会在屏幕较窄的设备上产生布局问题。 It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room. 最好改为使用“ ifRoom”来请求某项出现在操作栏中,但是当空间不足时,允许系统将其移至溢出区域。 However, it might be necessary to use this value if the item includes an action view that cannot be collapsed and must always be visible to provide access to a critical feature. 但是,如果该项目包含无法折叠且必须始终可见以提供对关键功能的访问的动作视图,则可能需要使用此值。

Reference 参考

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

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