BottomSheetDialog 在 Android 中没有正确地圆顶角

BottomSheetDialog not rounding top corners correctly in Android

我有一个自定义 BottomSheetDialog 并且我为其布局创建了具有顶角圆角矩形的可绘制对象。 BottomSheetDialog 布局背景是 Linearlayout 并且我将 drawable 应用于底部 sheet 的 layout.Top 角落正确舍入但是线性布局的另一个布局底部不是圆形(方形和白色见下图)并且它不在 layout.xml 文件中。它就像 square.I 顶部的圆角矩形无法摆脱那个正方形。

下面是我的自定义底部sheet样本

public abstract class EmployeeBottomSheetDialog extends BottomSheetDialog {

    private Context context;
    private Activity activity;
    private RecyclerView employeeRecyclerView;
    private EditText searchEditText;
    private DataBase dataBase;
    private ArrayList<Employe> employeeList = new ArrayList<>();
    private ArrayList<Employe> employeeSelectedList = new ArrayList<>();
    private SelectEmployeeAdapter selectEmployeeAdapter;
    private ImageButton closeSearchImageButton;

    public EmployeeBottomSheetDialog(@NonNull Context context, List<Employe> selectedEmployeeList) {
        super(context,R.style.BottomSheetDialogStyle);
        this.context = context;
        this.activity = (Activity) context;
        if(!selectedEmployeeList.isEmpty()){
            employeeSelectedList.clear();
            employeeSelectedList.addAll(selectedEmployeeList);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.employee_bottom_sheet_dialog);
    }
}

以下是我的风格

 <style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay</item>
        <item name="backgroundTint">@android:color/transparent</item>
        <item name="background">@android:color/transparent</item>
 </style>

下面是我的底sheetlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/transparent"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <LinearLayout
        android:id="@+id/dialog_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/bottom_sheet_background"
        android:padding="10dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_baseline_arrow_drop_down_24" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/between_two_views">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/search_wrapper"
                style="@style/textInputHint"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/search_employees">

                <EditText
                    android:id="@+id/search"
                    style="@style/textInputText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.textfield.TextInputLayout>

            <ImageButton
                android:id="@+id/close_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="15dp"
                android:background="@null"
                android:src="@drawable/ic_baseline_close_24"
                android:text="Button" />

        </RelativeLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/employee_recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/between_two_views"/>

    </LinearLayout>

</LinearLayout>

下面是我绘制的圆角

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

要解决这个问题,您需要在对话框样式(BottomSheetDialog 样式)中使用透明背景色:

<item name="android:colorBackground">@android:color/transparent</item>

经过数小时的搜索,我找到了答案。我只需要使用下面的样式

  <style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="android:windowIsFloating">false</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize</item>
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/bottom_sheet_background</item>
    </style>
 <style name="BottomSheetStyle" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/Model</item>
</style>

<style name="Model" parent="Widget.Design.BottomNavigationView">
    <item name="background">@drawable/bottom_sheet_layout</item>
</style>

对于那些寻求更简单 方法在所有状态下实现圆角的人,甚至 EXPANDED 状态,只需将 BottomSheetDialog 的背景属性定义为可绘制形状。

这是可绘制的形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/sheet_gray"/>
    <corners android:radius="24dp" />
</shape>

这是我的 BottomSheetDialog :

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bs_bottomsheet"
        android:background="@drawable/bottom_sheet_b"
        app:behavior_hideable="false"
        app:behavior_peekHeight="40dp"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

这将解决所有令人头疼的问题:

  • 设置自定义样式等等...
  • 想知道如何在 EXPANDED 状态下圆角 等...