无法将自定义主题应用于弹出菜单

时间:2015-01-28 21:48:42

标签: android xml android-studio popupmenu

为了我的生命。我无法在模拟时应用此自定义弹出式样式。它只显示默认的Theme.AppCompat.Light.DarkActionBar弹出式菜单,其中包含浅色背景,无论我做什么。

任何人都有任何想法?

我已经尝试过这个表单中的多个答案。如果我的代码下面 为此,我创建了一个虚拟应用程序来测试此功能。我已经确认所有的drawable都正确标记并存在。

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.erasedforprivacy.www.actionbarcolortest" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Example" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

styles.xml

<resources>

    <style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarItemBackground">@drawable/selectable_background_example</item>
        <item name="popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Example</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Example</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Example</item>
        <item name="actionModeBackground">@drawable/cab_background_top_example</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_example</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Example</item>

        <!-- Light.DarkActionBar specific -->
        <item name="actionBarWidgetTheme">@style/Theme.Example.Widget</item>

    </style>

    <style name="ActionBar.Solid.Example" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_example</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_example</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_example</item>
        <item name="progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <style name="ActionBar.Transparent.Example" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_example</item>
        <item name="progressBarStyle">@style/ProgressBar.Example</item>
    </style>

    <style name="PopupMenu.Example" parent="@style/Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
    </style>

    <style name="DropDownListView.Example" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="android:listSelector">@drawable/selectable_background_example</item>
    </style>

    <style name="ActionBarTabStyle.Example" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/tab_indicator_ab_example</item>
    </style>

    <style name="DropDownNav.Example" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
        <item name="android:background">@drawable/spinner_background_ab_example</item>
        <item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
        <item name="android:dropDownSelector">@drawable/selectable_background_example</item>
    </style>

    <style name="ProgressBar.Example" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:progressDrawable">@drawable/progress_horizontal_example</item>
    </style>

    <style name="ActionButton.CloseMode.Example" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="android:background">@drawable/btn_cab_done_example</item>
    </style>

    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Example.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Example</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Example</item>
    </style>

</resources>

MainActivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你没有使用合适的父母。试试这个:

<style name="Theme.Example" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    ....
    <item name="android:popupMenuStyle">@style/PopupMenu.Example</item>
    ....
</style>

<style name="PopupMenu.Example" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_example</item>
</style>

并且不要忘记在Manifest中应用这种风格:

<application
   ....
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/Theme.Example" >
   ....
</application>