首页 > 解决方案 > 如何使底部工作表对话框的角变圆?

问题描述

我试图使我的 BottomSheetDialog 的顶角变圆,但我在网上没有任何运气。这就是我希望它看起来的样子:

圆形模态底板

无论我尝试了什么,我都会得到这个:

非圆形模态底板

我已经尝试过这里的方法并使用 shapeAppearanceLargeComponent (我现在正在使用的)。

这是我的代码:

样式.xml

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    ...
    <item name="shapeAppearanceLargeComponent">@style/CustomShapeAppearanceBottomSheetDialog</item>
</style>

<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
</style>

底部导航菜单片段:

public class BottomNavMenuFragment extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_bottom_nav_drawer, container, false);
    }
}

这就是我展示片段的方式:

BottomNavMenuFragment navFragment = new BottomNavMenuFragment();
navFragment.show(getSupportFragmentManager(), navFragment.getTag());

我似乎没有做任何事情。有人能指出我正确的方向吗?

标签: javaandroidmaterial-designandroid-bottomsheetdialog

解决方案


按照以下步骤获得顶部圆角底片:

第 1 步:rounded_top_corners.xmldrawable文件夹中创建可绘制文件

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

第2步:创建以下样式styles.xml

 <style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/bottomSheetBackground</item>
    </style>

    <style name="bottomSheetBackground" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/rounded_top_corners</item>
    </style>

第 3 步:BottomNavMenuFragment在课堂上添加样式

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialogStyle);
    }

就是这样,样式将应用于底页。


推荐阅读