android - BottomSheetDialogFragment 的圆角

标签 android material-design bottom-sheet material-components-android material-components

我有一个自定义 BttomSheetDialogFragment,我想在底部 View 顶部有圆角

这是我的自定义类,它扩展了我想从底部显示的布局

View mView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.charge_layout, container, false);
    initChargeLayoutViews();
    return mView;
}

我还有这个 XML 资源文件作为背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <corners android:topRightRadius="35dp"
        android:topLeftRadius="35dp"
        />
    <solid android:color="@color/white"/>

    <padding android:top="10dp"
        android:bottom="10dp"
        android:right="16dp"
        android:left="16dp"/>
</shape>

问题是,当我将此资源文件设置为布局根元素的背景时,角落仍然没有圆角。

我不能使用下面的代码:

this.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.charge_layout_background);

因为它覆盖了 BottomSheetDialog 的默认背景,并且我的底部 View 上方不会有任何半透明的灰色。

最佳答案

创建自定义可绘制rounded_dialog.xml:

<?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="16dp"
        android:topRightRadius="16dp"/>

</shape>

然后使用drawable作为背景覆盖styles.xml上的bottomSheetDialogTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">       
    <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>

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

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

这将更改您应用的所有 BottomSheetDialogs。

关于android - BottomSheetDialogFragment 的圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43852562/

相关文章:

android - 如何防止 Eclipse 创建 fragment_main.xml

android - 约束布局内的回收器 View 问题

android - 当我按下后退按钮时,BottomSheet 不会折叠

flutter - Flutter:更改复选框的选中颜色

android - Cordova Geolocation 插件在 Android 上返回空位置对象

javascript - POST JSON 到 Node.JS ,语法错误 : Unexpected token %

android - 无法在 android 中解析编译 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'

java - 如何将 TextInputLayout 与 Theme.material 一起使用?

android - 覆盖 Android 工具栏默认填充是否被认为是不好的做法?

android - 如何将底部应用栏与顶部应用栏配对?