问题描述
我试图在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:
因此,如果将自定义视图用作操作,则可以对其进行充气(如果showAsAction
为never
,则不会按预期显示为操作).
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:
明确指出,溢出菜单不能显示自定义视图.
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吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!