如何在工具栏中制作菜单的圆角?

时间:2018-12-28 21:45:28

标签: android

这是我的toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 
    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"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_40sdp"
    android:background="@android:color/holo_red_dark"
    android:gravity="center_vertical"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:popupTheme="@style/PopUpTheme"/>

这是我在style.xml中自定义弹出菜单的方式

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="PopUpTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:popupBackground">@drawable/background_popup_menu</item>
    </style>
</resources>

background_popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white" />
    <corners android:radius="5dp" />
</shape>

这就是我最终得到的。

enter image description here

但是我需要这样做

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以更改溢出菜单的背景,该菜单在应用程序主题中定义 actionOverflowMenuStyle 属性。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   <item name="actionOverflowMenuStyle">@style/popupOverflowMenu</item>
</style>

使用:

  <style name="popupOverflowMenu" parent="@style/Widget.MaterialComponents.PopupMenu.Overflow">
    <item name="android:popupBackground">@drawable/my_mtrl_popupmenu_background</item>   
  </style>

然后使用类似以下内容的方法定义drawable/my_mtrl_popupmenu_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="?attr/colorSurface"/>

  <corners
      android:bottomLeftRadius="8dp"
      android:bottomRightRadius="8dp"
      android:topLeftRadius="8dp"
      android:topRightRadius="8dp"/>
  
</shape>

enter image description here

答案 1 :(得分:0)

但是您有圆角:)只需尝试增加<corners android:radius="25dp" />之类的radius值即可。