我想在工具栏上添加一个圆角,我在appTheme中使用了一个roundCornerDrawable,成功地使它变成了圆角。但我想要的是给一个在按钮上动态创建的popupMenu圆角,而不是通过充气在工具栏中创建的popupMenuu。
提前谢谢你,🙂!
发布于 2021-11-23 09:10:52
我找到了
this库帮助我获得我想要的圆角半径。
发布于 2021-11-23 16:19:37
可以创建包含圆角形状的xml文件。您可以使用此shape_background.xml文件将其用作弹出菜单中的背景。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="40dp" />
<!--If your shape requires only one solid color-->
<!--<solid
android:color="#FFFFFF" />-->
<size
android:width="82dp"
android:height="82dp" />
<!--Use android:dashWidth="2dp" and android:dashGap="2dp"
to add dashes to your stroke-->
<stroke
android:width="2dp"
android:color="#FFFFFF" />
<!--If you want to add padding-->
<!-- <padding
android:left="10dp"
android:top="20dp"
android:right="40dp"
android:bottom="8dp" />-->
</shape>
通过使用以下代码,您可以在弹出菜单中使用此xml文件作为背景。然后在styles.xml中声明的自定义样式中引用该可绘制的内容,例如
<style name="Widget.MPM.Menu.Dark.CustomBackground">
<item name="android:popupBackground">@drawable/mtrl_popupmenu_background</item>
</style>
最后一块拼图是在构建弹出菜单时使用该样式:
val popupMenu = popupMenu {
style = R.style.Widget_MPM_Menu_Dark_CustomBackground
// ... place your items here
}
here is the image with rounded popupmenu
如果你想在不做硬编码的情况下使用这个xml,你可以在弹出菜单布局xml文件中直接使用这个形状。
还可以将此XML文件用作弹出窗口布局XML中的背景:
android:background="@drawable/shape_background.xml"
https://stackoverflow.com/questions/70067288
复制相似问题