Styling Android PopupMenu Divider Lines

人走茶凉 提交于 2019-12-23 09:48:07

问题


I'm using a couple of PopupMenus in my app and they all working as expected

But I would like to have white lines separating the individual items, but I can't find where I'm meant to be setting this. I was hoping I could get to the underlying ListView but that doesn't seem to be possible. I can't see an style item that relates to the divider line either.

Is this possible, where/how should I be setting this?


回答1:


I've been able to update the divider colour on a PopupMenu using the following technique (based on clues from this answer https://stackoverflow.com/a/40017199/285190)

Create a style as follows...

<style name="popupMenuStyle" >
    <item name="android:textColor">#ffffff</item>
    <item name="android:itemBackground">#000000</item>
    <item name="android:divider">#eaeaea</item>
    <item name="android:dividerHeight">1dp</item>
</style>

Then when you create the menu create a context wrapper

Context wrapper = new ContextThemeWrapper(mContext, R.style.popupMenuStyle);
PopupMenu popup = new PopupMenu(wrapper, sourceView);

This does show a dividing line, BUT the height of the menu doesn't appear to be calculated correctly to include the new divider and a vertical scrollbar is displayed.

If anyone knows the reason please add a comment




回答2:


this is how I achieved it. colorBankground is to change the color of the line divider.

    <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();


来源:https://stackoverflow.com/questions/40694486/styling-android-popupmenu-divider-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!