问题
in my MainActivity, in onCreateOptionsMenu
method, I inflate a basic menu with four items (see menu_main.xml).
As it is possible to have rounded corners in DialogFragments, how can I get rounded corners for this menu?
As you can see in the screenshot, the menu appears as an overflow menu on top of the whole Activity (yes, the basic menu that Android gives you when tapping the three dots).
Screenshot here: MainActivity with menu opened
WHAT I TRIED
as for the Dialog, I added android:background="@drawable/basic_rounded_corners
to both the menu and the item nodes in menu_main.xml ---> not working (cause I don't know how to set the backgroundDrawable of the menu programmatically, if it is even possible like for the DialogFragments)
WHAT I WANT
I want to know how to get rounded corners on my menu.
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.beagleentertain.pillreminder.MainActivity"
android:background="@drawable/basic_rounded_corners">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/checkable_make7dayspause"
android:checkable="true"
android:checked="false"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/about_settings"
android:orderInCategory="100"
android:title="@string/text_menu_about"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/settings_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="ifRoom"
/>
<item
android:id="@+id/settings_share"
android:orderInCategory="100"
android:title="@string/action_share"
app:showAsAction="ifRoom"
/>
回答1:
First you need a toolbar in your activity in order to set a style for the popMenu.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/ThemeOverlay.MyTheme"
app:titleTextColor="#f0f0f0"/>
Then add this style and make sure you to set the colorBackground
to transparent so the rounded corners show correctly and then use any drawable shape as the background.
<style name="ThemeOverlay.MyTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:colorBackground">@android:color/transparent</item>
<item name="android:textColor">#000000</item>
<item name="android:background">@drawable/rounded</item>
</style>
If you want to make a completely custom window you can follow this tutorial in this Article. just make sure to add the rounded shape as background in the root view.
回答2:
In Kotlin You can use this library to create popup menus MaterialPopupMenu
This library allows creating simple popup menus programmatically with a nice type-safe builder syntax in Kotlin. Menus can be divided into separate sections with optional headers and contain icons.
You need to create a drawable with rounded corners e.g.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/mpm_material_grey_50"/>
<corners
android:radius="8dp"/>
<padding
android:bottom="8dp"
android:top="8dp"/>
</shape>
and then reference that drawable in a custom style declared in your styles.xml e.g.
<style name="Widget.MPM.Menu.Dark.CustomBackground">
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background</item>
</style>
The last piece of the puzzle is to use that style when building the popup menu:
val popupMenu = popupMenu {
style = R.style.Widget_MPM_Menu_Dark_CustomBackground
// ... place your items here
}
来源:https://stackoverflow.com/questions/48083496/android-rounded-corners-in-menu