Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 5.0中弹出菜单的样式设置_Android_Android Ui_Android 5.0 Lollipop_Material Design_Android Support Library - Fatal编程技术网

Android 5.0中弹出菜单的样式设置

Android 5.0中弹出菜单的样式设置,android,android-ui,android-5.0-lollipop,material-design,android-support-library,Android,Android Ui,Android 5.0 Lollipop,Material Design,Android Support Library,我正在为Android 5.0做准备,我正在使用最新的兼容性库,下面是我的风格 <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/theme_accent</item> <item name="colorAccent">@

我正在为Android 5.0做准备,我正在使用最新的兼容性库,下面是我的风格

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/theme_accent</item>
        <item name="colorAccent">@color/theme_accent_secondary</item>
    </style>

    <style name="AppThemeDark" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/theme_accent</item>
        <item name="colorAccent">@color/theme_accent_secondary</item>
    </style>
</resources>

@颜色/主题/口音
@颜色/主题\u重音\u次要
@颜色/主题/口音
@颜色/主题\u重音\u次要
(正在通过编程方式设置ActionBar颜色。)

现在,我希望溢出/弹出菜单具有与holo实现中相同的黑色背景,但我无法使其工作,以下是它的外观:

我尝试过设置
弹出菜单样式
,但没有成功


如何使弹出菜单变暗?

将属性弹出菜单添加到工具栏:

<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:background="@color/color_primary"
  app:theme="@style/Theme.AppCompat.Light"
  app:popupTheme="@style/Theme.AppCompat" />
<style name="MyToolBarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/green</item>
    <item name="popupTheme">@style/Theme.AppCompat.Light</item>
    <item name="theme">@style/Theme.AppCompat</item>
</style>

或者为工具栏定义新样式:

<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:background="@color/color_primary"
  app:theme="@style/Theme.AppCompat.Light"
  app:popupTheme="@style/Theme.AppCompat" />
<style name="MyToolBarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/green</item>
    <item name="popupTheme">@style/Theme.AppCompat.Light</item>
    <item name="theme">@style/Theme.AppCompat</item>
</style>

@颜色/绿色
@style/Theme.AppCompat.Light
@style/Theme.AppCompat

停止使用
操作栏
。如果你想把
工具栏
设置得像
操作栏
,请访问android开发者博客

它实际上在黑暗操作栏中提到了您的用例,并提供了以下代码:

<android.support.v7.widget.Toolbar
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”@dimen/triple_height_toolbar”
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

不是完整答案,但我目前发现:

在过去的版本中,您需要指定一个可绘制的(检查代码和教程)

显然,现在这是一种颜色。要修改它,您需要指定以下主题:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:actionBarPopupTheme">@style/popupNew</item>
    </style>

    <style name="popupNew" parent="android:ThemeOverlay.Material.Light">
        <item name="android:colorBackground">@color/red</item>
    </style>

</resources>

@样式/弹出窗口新建
@颜色/红色
如果应用于应用程序的主题仅为此,则此功能正常工作。
如果我在现有主题中添加了
android:actionBarPopupTheme
,它就不起作用了。我正在试图找出原因。

使用此样式解决了我的问题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/theme_accent</item>
    <item name="colorAccent">@color/theme_accent_secondary</item>
    <item name="actionBarStyle">@style/AbStyle</item>
    <item name="actionModeBackground">@color/actionmode_bg</item>
</style>

<style name="AbStyle" parent="Widget.AppCompat.Toolbar">
    <item name="elevation">2dp</item>
    <item name="displayOptions">homeAsUp|showTitle</item>
    <!--showHome-->
</style>

<style name="AppThemeDark" parent="Theme.AppCompat">
    <item name="colorAccent">@color/theme_accent_secondary</item>
    <item name="actionBarStyle">@style/AbStyle</item>
</style>


@颜色/主题/口音
@颜色/主题\u重音\u次要
@文体
@颜色/动作模式
2dp
homeAsUp | showTitle
@颜色/主题\u重音\u次要
@文体


我不得不使用Widget.AppCompat.Toolbar作为父actionBarStyle

这个问题已经在通过XML进行样式设置时得到了回答,但是我在这里添加了一个解释,说明如何自己解决这个和类似的样式设置问题

首先,这是使用AppCompat时的解决方案。在应用程序的style.xml中,将actionBarPopupTheme添加到主题中:

<style name="Theme.MyTheme" parent="@style/Base.Theme.AppCompat.Light.DarkActionBar">
    ...other stuff here

    <item name="actionBarPopupTheme">@style/Theme.MyTheme.ActionBarPopupTheme</item>
</style>

<style name="Theme.MyTheme.ActionBarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Light">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@android:color/black</item>
</style>

……这里还有其他东西
@style/Theme.MyTheme.ActionBarPopupTheme
@android:彩色/白色
@android:彩色/黑色
以下是我为实现此解决方案所采取的步骤(由于Android文档很差,需要做一些检测工作):

  • 在Android Studio中打开应用程序的style.xml
  • 在定义应用程序主题的行上,将屏幕光标放在父主题中(例如,单击@style/Base.theme.AppCompat.Light.DarkActionBar),然后按F4。这将带您到appcompat库中该样式的源代码
  • 在这种风格中,我看到了这一行:

    @style/ThemeOverlay.AppCompat.Light

    这看起来像是一个可以改变弹出窗口主题的地方。我在穷人中寻找“ActionBarPopuphtheme” Android开发者文档,并发现“一个主题的参考,应该用来 膨胀操作栏中小部件显示的弹出窗口”。所以这是值得玩玩的

  • 我将包含“actionBarPopupTheme”的appcompat行复制到my style.xml中,然后在这一行中用theme.MyTheme.actionBarPopupTheme替换项目的主题引用(上面粗体部分)

  • 在my style.xml中,我创建了名为Theme.myteme.ActionBarPopupTheme的新样式。我使用了从appcompat源代码复制的样式中使用的相同父级(上面的粗体部分)
  • 为了确保我的新弹出样式正常工作,我将父样式更改为meoverlay.AppCompat.Dark,然后在设备上运行并测试代码。弹出式风格改变了,所以现在我知道覆盖actionBarPopupTheme是正确的做法。然后我又改回了mooverlay.AppCompat.Light
  • 下一个挑战是确定要在Theme.MyTheme.ActionBarPopupTheme中覆盖哪些项目名称。我改变了文字和背景颜色。在某些情况下,要找到更改某事物样式的正确项目名称可能很棘手。查找不太明显的样式项名称的一种方法是查看appcompat xml文件中的样式定义(在上面的第二步中按F4键时打开的文件),不断向下搜索父样式(再次使用F4!),直到找到可以执行所需操作的内容。谷歌搜索在这方面也会有所帮助
我使用的是ActionBar,所以它对我没有多大帮助:(嗨,我们可以更改弹出菜单项的大小吗?为什么我要停止使用ActionBar?它不像是被弃用的。@动画在线工具栏是ActionBar的超集(简单地包装提取布局)你可以使用你已经做过的任何事情,甚至更多。ActionBar最终会被取代。正如你可能已经注意到的,当前的支持库在支持旧的ActionBar时有很多错误。我知道,我有一个非常复杂的应用程序,有很多片段和自定义导航,ActionBar做得很好,我不需要工具栏上的任何新功能,所以我现在将坚持使用AB,我感谢你的建议,但它们对我没有多大帮助。这是有效的。但是如何修改actionbar弹出窗口的阴影?正确的答案是,你的主题将该样式称为“actionBarPopupTheme”然后,在样式中,您必须为“ThemeOverlay”添加父级。我这样做了,效果很好。感谢您的回答!您是如何创建带有图标的溢出菜单的?溢出菜单的基本选项忽略图标。请尝试以下操作:安卓:showAsAction=“always”检查此选项以获得更好的解决方案