我可以在android.support.v7.widget.Toolbar的溢出菜单上使用actionLayout吗? [英] Can I use a actionLayout on the overflow menu of android.support.v7.widget.Toolbar?

查看:202
本文介绍了我可以在android.support.v7.widget.Toolbar的溢出菜单上使用actionLayout吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在android.support.v7.widget.Toolbar的溢出菜单上使用SwitchCompat小部件,但是我无法使其正常工作,它始终显示为空白.

I'm trying to use a SwitchCompat widget on the overflow menu of android.support.v7.widget.Toolbar but I just can't get it to work, it always appears blank.

这是我的菜单定义:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="com.oveflowtest.ScrollingActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never"/>
    <item android:id="@+id/test"
          app:actionLayout="@layout/testlayout"
          app:showAsAction="never"/>
</menu>

这是testlayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:text="test"/>
</LinearLayout>

此外,如果我将showAsAction更改为always而不是never,那么它在工具栏上的显示就很好,但是我不希望出现在工具栏上,我希望它在按下菜单时打开的溢出菜单上显示3点.

Also if I change showAsAction to always instead of never then it shows just fine on the toolbar but I don't want it there, I want it on the overflow menu that opens up when I press the 3 dots.

我知道有一个使用复选标记的选项,但是我遵循的设计要求进行切换.

I know there is an option to use a checkmark but the design I'm following calls for a switch.

谢谢.

推荐答案

不可能.

这是因为actionLayout仅在该项目显示为动作时才使用.它等效于setActionView,并且来自文档 :

That's because actionLayout is used only when the item is shown as action. It's equivalent to setActionView and, from the documentation:

为此菜单项设置操作视图.当该项目在父项中显示为动作时,将在UI中显示一个动作视图来代替自动生成的菜单项元素.

Set an action view for this menu item. An action view will be displayed in place of an automatically generated menu item element in the UI when this item is shown as an action within a parent.

因此,如果将自定义视图用作操作,则可以对其进行充气(如果showAsActionnever,则不会按预期显示为操作).

So, you can inflate a custom view if it's used as an action (if showAsAction is never, then it's not shown as action, as expected).

现在,在 ActionProvider 的文档中的其他地方,有以下内容:

Now, hidden somewhere else, in the documentation for ActionProvider, there's this:

以不允许自定义操作视图的方式显示菜单项时(例如,在溢出菜单中),ActionProvider可以执行默认操作.

When the menu item is presented in a way that does not allow custom action views, (e.g. in an overflow menu,) the ActionProvider can perform a default action.

明确指出,溢出菜单不能显示自定义视图.

It's explicitly stated that overflow menu can't display custom view.


如果您真的想以这种方式显示它,则可以使用伪造一个溢出菜单. PopupWindow ,但是在某些设备上甚至升级到Android后,您可能会失去与平台的一致性(假设…您使用了三个圆点来模拟溢出菜单,但后来又改回了正方形在Android上,您的应用看起来会很奇怪.还需要付出额外的努力才能使它运行,而Menu则很简单.


If you really want to display it that way, you can fake an overflow menu with PopupWindow, but then you may lose consistency with the platform in some devices or even after an upgrade to Android (let's say… you used three round dots to simulate the overflow menu, but later it is changed back to squares on Android, your app will look odd. There's also the extra effort to make it work while with a Menu it would be straightforward.

这篇关于我可以在android.support.v7.widget.Toolbar的溢出菜单上使用actionLayout吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆