更改弹出菜单样式 - 无法正常工作

时间:2016-09-02 08:47:05

标签: android background styles popupmenu

我正在尝试将样式应用于Android弹出菜单。按钮单击时显示菜单。在我的例子中,我想设置黑色菜单背景。

所以,我的菜单布局:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="Item 1"
        android:id="@+id/menu1">
    </item>

    <item
        android:title="Item 2"
        android:id="@+id/menu2">
    </item>

</menu>

接下来,我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.michal.popupmenu.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="btnClick"
        android:nestedScrollingEnabled="true"/>
</RelativeLayout>

点击按钮时显示菜单的代码:

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnClick(View view)
    {
        showMenu(view);

    }


    public void showMenu(View v)
    {
        PopupMenu popup = new PopupMenu(this, v);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }
}

自动生成样式xmle。我只添加了菜单样式来设置黑色菜单背景,这里是:

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@android:color/black</item>

</style>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

但是菜单背景仍为白色,应为黑色。任何想法有什么不对?

[编辑] 根据评论,更新代码:

<resources>

    <style name="PopupMenu" parent="@style/Widget.AppCompat.Light.PopupMenu">
        <item name="android:popupBackground">#FF4081</item>
    </style>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>



</resources>

主要活动:

public void showMenu(View v)
    {
        Context wrapper = new ContextThemeWrapper(getApplicationContext(), R.style.PopupMenu);
        PopupMenu popup = new PopupMenu(wrapper, v);

        // This activity implements OnMenuItemClickListener
        //popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }

结果不是我期望的结果

result

菜单背景仍然是黑色,而不是我想要设置的颜色。

4 个答案:

答案 0 :(得分:7)

我也试过上面提到的解决方案,但我的popupmenu颜色没有改变,所以我最终做了如下:

1.使用所需颜色处理自定义可绘制的内容,如下所示:

popup_color_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
  <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true">
     <color
         android:color="@color/colorPrimary"/>
      </item>
      <item>
          <color
              android:color="#655611"/>
      </item>
  </selector>

添加 state_pressed 以获得选择效果

2.在我的styles.xml中,添加了以下代码:

<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:itemBackground">@drawable/popup_color_drawable</item>
</style>

我使用Theme.AppCompat.Light.DarkActionBar作为app的基本主题。

3.然后在我的活动中

public void showpopup(View view){
    Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
    PopupMenu popup = new PopupMenu(wrapper, view);
    popup.inflate(R.menu.popup_menu);
    popup.show();
}

以下是我使用此代码获得的结果 enter image description here

希望这会对你有所帮助。

答案 1 :(得分:4)

你应该试试这个:

 public void showMenu(View v)
    {
        Context wrapper = new ContextThemeWrapper(getContext(), R.style.PopupMenu);
        PopupMenu popup = new PopupMenu(wrapper, v);
        popup.inflate(R.menu.popup_menu);
        popup.show();
    }

答案 2 :(得分:2)

在原始问题中,您需要做的就是改变:

<item name="android:popupBackground">#FF4081</item>

为:

 <item name="android:itemBackground">@android:color/white</item>

答案 3 :(得分:0)

我的问题是使用了错误的弹出式充气机。更改自:

popup.menuInflater.inflate(R.menu.menu_language, popup.menu)

popup.inflate(R.menu.menu_language)

有效!