如果showAsAction = never,则“自定义布局”菜单项显示为空

Sah*_*ave 5 android android-layout

我正在尝试使用以下代码将SwitchCompat添加到“溢出”菜单

main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       android:id="@+id/menu_dummy_content"
       android:title=""
       android:actionViewClass="android.support.v7.widget.SwitchCompat"
       app:showAsAction="never"
       app:actionLayout="@layout/menu_item_switch"/>

    <!-- TODO: Delete this after bug fix-->
    <item
       android:id="@+id/menu_dummy_content_2"
       android:title="Second Title"
       app:showAsAction="never"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

menu_item_switch的布局为:

<android.support.v7.widget.SwitchCompat
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/menu_item_switch_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="@string/menu_item_dummy_tasks"
    android:layout_centerVertical="true"/>
Run Code Online (Sandbox Code Playgroud)

如果我执行app:showAsAction =“ ifRoom”或其他任何操作,则开关看起来可以正常运行,并且侦听器可以正常运行。但是,一旦我使showAsAction成为从来没有。开关视图变为白色/空,但onOptionsItemSelected表示单击的项目为menu_dummy_content。

我还尝试在自定义布局中仅使用TextView,它也为empty。我想念什么吗?

iMD*_*oid 1

由于您将 showAsAction 属性设置为 never,因此这些菜单项将永远不会显示为操作布局。

我相信您需要在此处添加标题文本...

 <item
   android:id="@+id/menu_dummy_content"
   android:title=""
   android:actionViewClass="android.support.v7.widget.SwitchCompat"
   app:showAsAction="never"
   app:actionLayout="@layout/menu_item_switch"/>
Run Code Online (Sandbox Code Playgroud)

像这样...

android:title="First Title"
Run Code Online (Sandbox Code Playgroud)