如何在android中实现OverflowMenuButton小部件

时间:2015-03-03 08:04:13

标签: android android-5.0-lollipop

我正在尝试将android工具栏作为操作栏实现。我的操作栏应包含一个徽标作为标题和溢出菜单。溢出菜单图标颜色为绿色。我使用了一个小部件 OverflowMenuButton 但添加此内容后会出现一些异常。我不知道如何正确使用这个小部件。

这是我的代码

<android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="@color/white"
android:gravity="center">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:focusable="false"
    android:longClickable="false"
    android:src="@drawable/logo_multi"/>
  <OverflowMenuButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/green"/>

 </android.support.v7.widget.Toolbar>

我发布了一些错误行

at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class OverflowMenuButton
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
         at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at com.ragpicker.screen.StartScreen.onCreate(StartScreen.java:35)
        at android.app.Activity.performCreate(Activity.java:5541)
  Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.OverflowMenuButton" on path: DexPathList[[zip file "/data/app/com.ragpicker-669.apk"],nativeLibraryDirectories=[/data/app-lib/com.ragpicker-669, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67)
        at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)

在oncreate中,

 setContentView(R.layout.screen_startscreen);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

1 个答案:

答案 0 :(得分:0)

我发现了solution1solution2这个问题:

1-不要调用setSupportActionBar(mToolbar);更多,而是直接使用工具栏

2-通过调用ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(getApplicationContext()))检查设备是否有硬件菜单按钮;

3-如果设备有菜单按钮我在onCreateOptionsMenu中返回true,否则我在工具栏中膨胀菜单