本文介绍了Android:菜单中的圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 MainActivity 中,在 onCreateOptionsMenu 方法中,我扩充了一个包含四个项目的基本菜单(请参阅 menu_main.xml).
由于 DialogFragments 中可能有圆角,我怎样才能获得此菜单的圆角?

正如您在屏幕截图中所看到的,菜单显示为整个 Activity 顶部的溢出菜单(是的,当点击三个点时,Android 为您提供的基本菜单).
此处截图:打开菜单的 MainActivity

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

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)

我想要什么
我想知道如何在我的菜单上获得圆角.

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"
    />

推荐答案

首先,您需要在活动中使用工具栏,以便为 popMenu 设置样式.

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"/>

然后添加此样式并确保将 colorBackground 设置为透明,以便圆角正确显示,然后使用任何可绘制形状作为背景.


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.

这篇关于Android:菜单中的圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:23