gpt4 book ai didi

android - 溢出菜单的工作原理

转载 作者:行者123 更新时间:2023-11-30 02:57:34 24 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加一个溢出菜单,但它并没有像我希望的那样工作。

事实上,溢出按钮并没有显示在屏幕右上角的操作栏中,但是当我点击我的设备 (Samsung Galaxy S3) 的设置按钮时它起作用了。屏幕底部会出现一种选项卡,当我选择它时,屏幕中间会显示一个带有四项菜单的弹出窗口。

我想了解如何管理它,以获得与图中数字三相同的结果:

enter image description here

list .xml :

 <uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light">

样式.xml :

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">

res > menu > menu_maps_styles.xml(这是我的溢出菜单):

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

<!-- Normal map -->
<item
android:id="@+id/action_normal_map"
GoogleMapAndroid:showAsAction="never"
android:title="@string/action_normal_map"/>

<!-- Satellite map -->
<item
android:id="@+id/action_satellite_map"
GoogleMapAndroid:showAsAction="never"
android:title="@string/action_satellite_map"/>

<!-- Terrain map -->
<item
android:id="@+id/action_terrain_map"
GoogleMapAndroid:showAsAction="never"
android:title="@string/action_terrain_map"/>

<!-- Hybrid map -->
<item
android:id="@+id/action_hybrid_map"
GoogleMapAndroid:showAsAction="never"
android:title="@string/action_hybrid_map"/>

</menu>

主要 Activity .java :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_maps_styles, menu);

return true;
}

最佳答案

更新

根据您的原始帖子和下面评论中的讨论,解决方案是按如下方式更改您的样式:

<style name="AppBaseTheme" parent="@style/Theme.Holo.Light">  

这解决了您的问题,因为您的项目不需要 AppCompat 库:

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />

但是,如果您想使用 AppCompat,我会在下面给出我的原始答案,我建议您阅读以下内容:Setting Up the Action Bar: Support Android 2.1 and Above .
好的开发者。


原始答案

如您在文档中所见:

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

那么,你的布局必须是:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Normal map -->
<item
android:id="@+id/action_normal_map"
yourapp:showAsAction="never"
android:orderInCategory="1"
android:title="@string/action_normal_map"/>

<!-- Satellite map -->
<item
android:id="@+id/action_satellite_map"
yourapp:showAsAction="never"
android:orderInCategory="2"
android:title="@string/action_satellite_map"/>

<!-- Terrain map -->
<item
android:id="@+id/action_terrain_map"
yourapp:showAsAction="never"
android:orderInCategory="3"
android:title="@string/action_terrain_map"/>

<!-- Hybrid map -->
<item
android:id="@+id/action_hybrid_map"
yourapp:showAsAction="never"
android:orderInCategory="4"
android:title="@string/action_hybrid_map"/>
</menu>

( See the details here ) onCreateOptionsMenu 方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_maps_styles, menu);
return true;
}

您的更多项目 (id a_More) 是无用的,因为溢出菜单按钮是通过类的扩展实现的。这是一个自动小部件。
此外,确保您的Activity extends ActionBarActivity 并且您有正确的导入:

  • 导入 android.view.Menu;
  • 导入 android.view.MenuItem;
  • 导入 android.view.MenuInflater;
  • 导入 android.support.v7.app.ActionBar;

同时确保您已更新最新版本的 SDK。
您的主题需要如下所示:

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light">  

现在,如果需要,您可以在 API 7 及更高版本上使用 AppCompat:

<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />

关于android - 溢出菜单的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com