java - 在卡片 View 中使用时,Android 中带有圆角的 Persistent Bottom Sheet 会崩溃

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

我在 android 中创建了一个持久的 Bottom Sheet ,目的是显示一个包含有关位置的附加信息的 ListView。我希望床单有圆角。我得到了大量模态对话框的结果,但没有得到持久的结果。有可能还是应该使用模态版本?

正如此处的答案所建议的那样,我尝试将其包装在卡片 View 中,但是当我尝试打开 fragment 时应用程序崩溃。


<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".fragments.MapFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:cardCornerRadius="20dp">

            <fragment
                android:id="@+id/autocomplete_fragment"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:layout="@layout/places_autocomplete_item_powered_by_google" />

        </androidx.cardview.widget.CardView>

        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="450dp"
        app:cardCornerRadius="24dp"
        app:cardElevation="8dp"
        app:layout_behavior="@string/bottom_sheet_behavior"
        app:behavior_hideable="true"
        app:behavior_peekHeight="55dp">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tvBottomSheet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/placeholder_text"
                android:textColor="@color/colorCommon_BLACK"
                android:textSize="37sp" />

        </androidx.core.widget.NestedScrollView>

    </androidx.cardview.widget.CardView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

随着新的Material Component图书馆你可以customize the shape您的组件使用 shapeAppearanceOverlay 属性。

你可以使用类似的东西:

  <!-- BottomSheet -->
  <style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
    <item name="shapeAppearanceOverlay">@style/CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet</item>
    ....
  </style>

  <style name="CustomShapeAppearanceOverlay.MaterialComponents.BottomSheet" parent="">
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>

然后,您可以将此样式应用于您的单个组件或主题应用程序。
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="bottomSheetStyle">@style/CustomBottomSheet</item>
  </style>

如果您使用的是非模态 Bottom Sheet ,只需应用样式即可。例如:
<LinearLayout
    android:id="@+id/standardBottomSheet"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
    style="?attr/bottomSheetStyle" 
    ...>

enter image description here

关于java - 在卡片 View 中使用时,Android 中带有圆角的 Persistent Bottom Sheet 会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56682333/

相关文章:

android - 更新通知文本,而不是整个通知

android - GPS 或 ACCELEROMETER 哪个更适合计算赛道?

android - 自定义 Android 图像裁剪

jquery - Angular 下拉菜单和选项卡的问题

c# - WPF TabControl with Materials Design(没有 Dragablz)

java - 可以使用重复继承来缩小类吗?

java - 在Java中将文本文件读入char[][]数组

java - 如果删除空格,DateTimeFormatter 解析带有可选时间部分的字符串将失败

java - 单击复选框后面板重新加载

android - 菜单项未显示为操作 android.support.v7.widget.Toolbar