web-dev-qa-db-ja.com

ポップアップメニューの背景色を変更する

注:1時間検索して、stackoverflowによってすでに提供されているすべてのソリューションを試しました。

テーマオーバーレイについて勉強しています。アクションバーアイコンをクリックするとポップアップメニューが開くサンプルアプリを作りました。これが私のstyles.xmlです

<resources>

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

    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>



    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="Android:textColorPrimary">@color/colorAccent</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
        <!-- added all to see which one will work.-->
        <item name="Android:popupMenuStyle">@style/PopupMenu</item>
        <item name="Android:itemBackground">@color/colorAccent</item>
        <item name="Android:colorBackground">@color/colorAccent</item>

    </style>

    <style name="PopupMenu" parent="@Android:style/Widget.PopupMenu">
        <item name="Android:popupBackground">@color/colorAccent</item>
    </style>

</resources>

これが私のツールバースタイルです。

   <Android.support.design.widget.AppBarLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:theme="@style/AppTheme.AppBarOverlay">

        <Android.support.v7.widget.Toolbar
            Android:id="@+id/toolbar"
            Android:layout_width="match_parent"
            Android:layout_height="?attr/actionBarSize"
            Android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </Android.support.design.widget.AppBarLayout>

popupThemestyles.xmlにあるものに設定しました。現在、ポップアップメニューの背景色を変更したいのですが、現在は白色です。

enter image description here

これがコードです。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.standard_menu){
            showPopupMenu(item);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showPopupMenu(MenuItem item) {
        PopupMenu p = new PopupMenu(this, findViewById(item.getItemId()));
        p.inflate(R.menu.pop_menu);
        p.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(MainActivity.this, "clicked.", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        p.show();
    }
8
mallaudin

OPのカスタムポップアップスタイルが適用されない理由(背景だけでなく、テキストの色など)も実際には説明されていないため、受け入れられた回答には満足していませんでした。そのため、私は独自の実験を行いました。

Toolbarによって作成されたポップアップ(メニュー項目がある場合)とPopupMenuを使用して自分で表示するポップアップには違いがあることに注意してください。これらは、さまざまなテーマ属性によって管理されます。また、2つのPopupMenuクラスがあることに注意してください: _Android.widget.PopupMenu__Android.support.v7.widget.PopupMenu_

明示的に表示するPopupMenusのスタイル設定に必要なテーマ属性は、_Android:popupMenuStyle_またはpopupMenuStyleです。カスタムスタイルを適切に適用するには、いくつかのオプションがあります。

(1)アクティビティ(またはアプリ)のテーマで_Android:popupMenuStyle_を使用します

_<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- if using Android.widget.PopupMenu -->
    <item name="Android:popupMenuStyle">@style/PopupMenu</item>
    <!-- if using Android.support.v7.widget.PopupMenu -->
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style/>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
    <item name="Android:popupBackground">@color/popupBackground</item>
</style>

PopupMenu popup = new PopupMenu(this, anchorView);
_

これは、レイアウトファイルに何も追加する必要がないことに注意してください。

(2)ContextThemeWrapperを使用します

_<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- nothing special -->
</style/>

<style name="CustomPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
    <!-- if using Android.widget.PopupMenu -->
    <item name="Android:popupMenuStyle">@style/PopupMenu</item>
    <!-- if using Android.support.v7.widget.PopupMenu -->
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
    <item name="Android:popupBackground">@color/popupBackground</item>
</style>

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomPopupTheme);
PopupMenu popup = new PopupMenu(ctw, anchorView);
_

ContextThemeWrapperの作成時に、これが_R.style.PopupMenu_を直接使用しないことに注意してください。これは少し回り道のようですが、ポップアップテーマをアクティビティやアプリのテーマから分離したい場合に便利です(たとえば、特別なテーマが必要なのは一部のポップアップのみです)。

(3)AppBarLayoutのコンテキストを使用

_<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- nothing special -->
</style/>

<style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
    <!-- if using Android.widget.PopupMenu -->
    <item name="Android:popupMenuStyle">@style/PopupMenu</item>
    <!-- if using Android.support.v7.widget.PopupMenu -->
    <item name="popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
    <item name="Android:popupBackground">@color/popupBackground</item>
</style>

<style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Dark">
    <!-- changes the background of the Toolbar's popup -->
    <item name="Android:colorBackground">@color/popupBackground</item>
</style>


<Android.support.design.widget.AppBarLayout
    Android:id="@+id/appbar"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:theme="@style/AppBarOverlay">

    <Android.support.v7.widget.Toolbar
        Android:id="@+id/toolbar"
        Android:layout_width="match_parent"
        Android:layout_height="?attr/actionBarSize"
        Android:background="?attr/colorPrimary"
        app:popupTheme="@style/PopupOverlay"/>

</Android.support.design.widget.AppBarLayout>


AppBarLayout appBar = (AppBarLayout) findViewById(R.id.app_bar);
PopupMenu popup = new PopupMenu(appBar.getContext(), anchorView);
_

AppBarのテーマオーバーレイはすでにあるので、それを使用してポップアップテーマの参照を保持できます。これは、少なくとも現在のレイアウトが与えられていれば、ツールバーのコンテキストでも機能しますが、ToolbarのポップアップではなくPopupMenuに影響するため、_app:popupTheme_は実際にはここでは関係ありません。また、これが上記のオプション2とどの程度類似しているかに注意してください。これにより、内部で_Android:theme_属性がどのように機能するかがわかります;)

私の実験では、_Android:itemBackground_は、PopupOverlayスタイルで_Android:colorBackground_の代わりに使用した場合にのみ機能しました。ただし、_Android:colorBackground_を使用することをお勧めします。これにより、ポップアップのウィンドウの色が変更され、丸みを帯びたコーナーと選択可能なアイテムのハイライト/アイテムのリップルがそのまま残ります。

23
Karakuri

やってみよう

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="Android:colorBackground">@color/white</item>
        <item name="Android:textColor">@color/grey_900</item>
    </style>
1
Cícero Moura

これは私のために働いた

<item name="Android:itemBackground">@color/primary</item>

それをメインスタイルに挿入します。

1
Harian Manda

サポートv7ライブラリのPopupMenuには「popupMenuStyle」を使用し、通常のPopupMenuには「Android:popupMenuStyle」を使用します

1

上記の解決策はどれも私にとってうまくいかなかったので、これが私がそれを修正した方法です:

まず、アクティビティの新しいテーマを作成します。

<style name="Theme.YourTheme" parent="Theme.AppCompat">
    <item name="Android:itemBackground">@color/white</item>
    <item name="Android:textColor">@color/black</item>
</style>
  1. アイテムの背景を白くするには

    <item name="Android:itemBackground">@color/white</item>
    
  2. テキストの色を黒にするには

    <item name="Android:textColor">@color/black</item>
    

最後に、マニフェストを使用してテーマをアクティビティに適用します。

    <activity Android:name=".YourActivity"
        Android:theme="@style/Theme.YourTheme"/>
0
cht

AppCompatツールバーのpopupThemeを使用する

 <Android.support.v7.widget.Toolbar
        Android:id="@+id/toolbar"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        app:popupTheme="@style/MyTheme"/>

スタイルで:

  <style name="MyTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="Android:colorBackground">@color/colorPrimary</item>
    <item name="Android:textColor">@color/white</item>
</style>

それがうまくいくことを願っています。

0
Minion

ポップアップメニュースタイルをur AppThemeに追加します。

<style name="AppTheme" parent="Android:Theme.Light">
    <item name="Android:popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="@Android:style/Widget.PopupMenu">
    <item name="Android:popupBackground">@Android:color/white</item>
</style>

manifest.xml:

 <application
    Android:allowBackup="true"
    Android:icon="@drawable/ic_launcher"
    Android:label="@string/app_name"
    Android:theme="@style/AppTheme" >
.............
</application>

うまくいくことを願っています。

0
Manikandan K
<style name="YOURSTYLE" parent="Widget.AppCompat.PopupMenu">
    <item name="Android:textColor">@Android:color/white</item>
    <item name="Android:itemBackground">@Android:color/holo_red_light</item>
</style>

Context wrapper = new ContextThemeWrapper(this, R.style.YOURSTYLE);
PopupMenu popup = new PopupMenu(wrapper, view);

それはあなたを助けるかもしれません

0
shekhar pande

このようにPopupOverlay内でcolorPrimaryを使用してみてください

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="colorPrimary">@color/blue_ivy</item>
</style>
0
John Kalimeris