弹出菜单样式问题

时间:2016-11-21 16:03:45

标签: android android-layout listview android-studio

我正在开发一个应用程序,其中有一个列表和一个弹出菜单。但我的应用程序有黑色背景颜色,文本颜色为白色。但我的弹出菜单背面颜色是白色,文字颜色是白色。我尝试了所有可行的方法,但它没有用。这是我的代码。

Style.xml     

<attr name="iconColor" format="reference" />
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/back_color</item>
    <item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
    <item name="android:textColor">@color/White</item>

    <item name="android:textSize">12dp</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="iconColor">@android:color/white</item>
    <item name="android:popupBackground">@color/back_color</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
    <item name="colorAccent">@android:color/holo_red_light</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >

    <item name="android:panelBackground">@color/back_color</item>
    <item name="android:textColor">@color/black</item>
</style>

<style name="StyledScrollerTextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textSize">24sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.TextAppearance">
    <item name="android:textColor">@color/White</item>
    <item name="android:textSize">12dp</item>
</style>

<style name="AppTheme.TextAppearance.Subtitle">
    <item name="android:textColor">@color/White</item>
    <item name="android:textSize">10dp</item>
</style>

<style name="PopupMenu">
    <item name="android:panelBackground">@color/back_color</item>
    <item name="android:textColor">@color/White</item>
</style>

<style name="Theme.AppCompat.Light.NoActionBarCustom" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="colorAccent">@android:color/holo_red_dark</item>
</style>

以下是回收站视图适配器

 @Override
public void onBindViewHolder(ItemHolder itemHolder, int i) {
    Song localItem = arraylist.get(i);

    itemHolder.title.setText(localItem.title);
    itemHolder.artist.setText(localItem.artistName);


    setOnPopupMenuListener(itemHolder, i);

}

private void setOnPopupMenuListener(ItemHolder itemHolder, final int position) {

    itemHolder.popupMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context wrapper = new ContextThemeWrapper(mContext, R.style.PopupMenu);
            final PopupMenu menu = new PopupMenu(wrapper, v);

            menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.popup_play:

                            break;
                        case R.id.popup_next:

                            break;

                    }
                    return false;
                }
            });
            menu.inflate(R.menu.popup_song);

            menu.show();
        }
    });
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

  

阅读评论后,您需要contextual menu

PopupMenu继承您的Widget.PopupMenu

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

我建议将工具栏包装在AppBarLayoutAppBarLayout theme设置菜单样式。

 <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/app_bar"
        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>

以下是款式。

   <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Light">
        <!-- text color for toolbar and popup menu-->
        <item name="android:textColorPrimary">@color/colorAccent</item>
        <item name="popupMenuStyle">@style/AppTheme.PopupMenu</item>
        <item name="android:popupMenuStyle">@style/AppTheme.PopupMenu</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" />

    <style name="AppTheme.PopupMenu" parent="@android:style/Widget.PopupMenu">
        <!-- background color of popup menu-->
        <item name="android:popupBackground">@color/colorBlack</item>
    </style>

最后,使用AppBarLayout上下文创建弹出菜单。

   AppBarLayout bar= (AppBarLayout) findViewById(R.id.app_bar);
        PopupMenu p = new PopupMenu(bar.getContext(), findViewById(item.getItemId()));

有关详情,请参阅this answer。