样式自定义弹出菜单

时间:2015-07-17 10:30:55

标签: android popupmenu

我有一个图像按钮,在按下时会显示一个弹出菜单。

public void showMenu (View v){
    PopupMenu popup = new PopupMenu(getBaseContext(), v);
    popup.getMenuInflater().inflate(R.menu.menu_launch_page, popup.getMenu());
    popup.show();

}

生成的菜单背景较暗。如何设置弹出窗口的样式,以便更改背景颜色以及选择项目时的颜色。我正在使用android:Theme.Holo.Light.NoActionBar,因此任何地方都不会涉及Actionbar。

3 个答案:

答案 0 :(得分:3)

好的,你可以在这里尝试这个,你也可以设置自定义适配器

yourButtonViewObject是您单击以显示此弹出窗口的按钮

ListPopupWindow listPopupWindow = new ListPopupWindow(
            ListPopupWindowAppActivity.this);

//here you can set your custom adapter aswell
        listPopupWindow.setAdapter(new ArrayAdapter(
            ListPopupWindowAppActivity.this,
            R.layout.list_item, passyourarrayhere));
        listPopupWindow.setAnchorView(yourButtonViewObject);
        listPopupWindow.setWidth(300);
        listPopupWindow.setHeight(400);

listPopupWindow.setModal(true);
        listPopupWindow.setOnItemClickListener(
            ListPopupWindowAppActivity.this);

//when you click on yourButtonViewObject show listPopupWindow like this
listPopupWindow.show();

//and in onItemClick overiden method do similar as you do in listview

答案 1 :(得分:1)

这可以帮到你

<resources>

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">

    <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
    <item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>

    <!-- Change Overflow Menu ListView Item Height & Property -->
    <item name="android:listPreferredItemHeightSmall">45dp</item>
    <item name="android:listPreferredItemPaddingLeft">5dp</item>
    <item name="android:listPreferredItemPaddingRight">5dp</item>
</style>

<!-- Change Overflow Menu ListView Divider Property -->
<style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
    <item name="android:divider">#FF0000</item>
    <item name="android:dividerHeight">2dp</item>
</style>

<!-- Change Overflow Menu ListView Text Size and Text Size -->
 <style name="PopupMenuTextView" parent="@style/android:Theme.Holo.Light">
    <item name="android:textColor">#00FF00</item>
    <item name="android:textSize">28dp</item>
</style>

 <!-- Change Overflow Menu Background -->
 <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">#888888</item>
</style>

</resources>

答案 2 :(得分:1)

我就这样做了:

<style name="PopupMenu">
    <item name="android:itemBackground">@color/background_medium_gray</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:colorBackground">@color/BackgroundGray</item>
    <item name="android:dividerHeight">1dp</item>
</style>
Context context = new ContextThemeWrapper(getActivity(), R.style.PopupMenu);
final PopupMenu popupMenu = new PopupMenu(context, view);

final MenuInflater menuInflater = popupMenu.getMenuInflater();