简体   繁体   English

Android:菜单中的圆角

[英]Android: Rounded corners in Menu

in my MainActivity, in onCreateOptionsMenu method, I inflate a basic menu with four items (see menu_main.xml ).在我的 MainActivity 中,在onCreateOptionsMenu方法中,我用四个项目扩充了一个基本菜单(请参阅menu_main.xml )。
As it is possible to have rounded corners in DialogFragments, how can I get rounded corners for this menu?由于 DialogFragments 中可能有圆角,我怎样才能获得此菜单的圆角?

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).正如您在屏幕截图中看到的,菜单显示为整个 Activity 顶部的溢出菜单(是的,Android 在点击三个点时为您提供的基本菜单)。
Screenshot here: MainActivity with menu opened此处的屏幕截图:打开菜单的 MainActivity

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)至于对话框,我在 menu_main.xml 中的菜单和项目节点都添加了android:background="@drawable/basic_rounded_corners ---> 不工作(因为我不知道如何以编程方式设置菜单的 backgroundDrawable ,如果它甚至可能像 DialogFragments 一样)

WHAT I WANT我想要什么
I want to know how to get rounded corners on my menu.我想知道如何在我的菜单上获得圆角。

menu_main.xml菜单_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"
    />

First you need a toolbar in your activity in order to set a style for the popMenu.首先,您的活动中需要一个工具栏,以便为 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.然后添加此样式并确保将colorBackground设置为透明,以便圆角正确显示,然后使用任何可绘制形状作为背景。

 <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.只需确保在根视图中添加圆形作为背景。

In Kotlin You can use this library to create popup menus MaterialPopupMenuKotlin 中你可以使用这个库来创建弹出菜单MaterialPopupMenu

This library allows creating simple popup menus programmatically with a nice type-safe builder syntax in Kotlin.这个库允许在 Kotlin 中使用漂亮的类型安全构建器语法以编程方式创建简单的弹出菜单。 Menus can be divided into separate sections with optional headers and contain icons.菜单可以分为带有可选标题的单独部分,并包含图标。

You need to create a drawable with rounded corners eg您需要创建一个带有圆角的可绘制对象,例如

<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 eg然后在styles.xml中声明的自定义样式中引用该drawable,例如

<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
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM