更改appCompat v7工具栏中的PopupMenu背景imageButton onClick

时间:2015-05-21 12:04:25

标签: android android-appcompat android-toolbar popupmenu appcompat-v7-r21

我想更改工具栏内imageButton的onClick事件触发的弹出菜单的默认背景颜色。这是我的代码:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:app1="http://schemas.android.com/apk/res/com.bisan.proj.android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    android:background="?attr/colorPrimary" >
    <RelativeLayout
        android:id="@+id/myCustomActionBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout 
            android:id="@+id/optionsContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignParentEnd="true">
            <android.support.v7.widget.SearchView
                android:id="@+id/ItemsSearchView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app1:closeIcon="@drawable/search_close_icon"
                app1:searchIcon="@drawable/search_icon" />
            <Spinner 
                android:id="@+id/categoryChooser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:popupBackground="@color/toolbar_background_color" />
            <ImageButton
                android:id="@+id/displayMethodChooser"
                android:layout_width="25dp"
                android:layout_height="35dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:onClick="showDisplayMethodPopup"
                android:background="@drawable/display3" />
        </LinearLayout>


        <LinearLayout 
            android:id="@+id/logoContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignParentStart="true">
            <ImageView
                android:id="@+id/bisanLogo"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:src="@drawable/ic_launcher" />

            <TextView 
                android:id="@+id/activityTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="15sp"
                android:textColor="#FFFFFF"
                android:text="@string/app_name"/>
        </LinearLayout>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

id = displayMethodChooser的imageButton会触发以下函数:

public void showDisplayMethodPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.main, popup.getMenu());
    popup.show();

    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {            
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            return false;
        }
    });
}

工具栏的颜色设置为toolbar_background_color,即colorPrimary。我希望popupMenu与工具栏具有相同的颜色!

应用于该应用的主题是:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Application Theme -->
    <style name="BisanAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/toolbar_background_color</item>
    </style>

</resources>

我几乎尝试了一切,但没有任何效果!

0 个答案:

没有答案