繁体   English   中英

如何向底部工作表视图添加阴影?

[英]How can I add a shadow to bottom sheet view?

截至目前,使用来自 Android 设计库的官方底部表单组件实现的顶部边缘不显示阴影。 但是对于我在各种模型和 Material Design 规范中看到的内容,底部表单包含某种离散的阴影。

我认为阴影将有助于将底部工作表与主布局分开,特别是如果设置了 peek 值和/或底部工作表始终可见。 否则它只会与主要布局及其项目混合在一起。

我已经尝试了ViewCompat.setElevation(bottomSheet, 5); 并将android:elevation="5dp"设置为 XML 中的视图,但没有成功。

Material Design 规范中的底片示例

我知道阴影形状的外观与高程不同 - 但至少尝试一下。 诀窍是使用app:layout_anchor将阴影剪辑到底部工作表。

活动_main.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<View
    android:id="@+id/shadow"
    android:layout_width="match_parent"
    android:layout_height="16dp"
    android:background="@drawable/shape_gradient_top_shadow"
    app:layout_anchor="@id/bottom_sheet" />

<FrameLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:clipToPadding="false"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />

</android.support.design.widget.CoordinatorLayout>

shape_gradient_top_shadow.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="90"
    android:endColor="@android:color/transparent"
    android:startColor="#64000000"/>
</shape>

看起来像这样:

底片阴影

编辑

使用自定义ShadowView获得更好的结果:

然后您可以执行以下操作:

<ShadowView
    android:id="@+id/shadow"
    android:layout_width="match_parent"
    android:layout_height="16dp"
    android:gravity="bottom"
    app:layout_anchor="@id/bottom_sheet" />

对于 API 级别 21 及更高版本,请在父视图中设置以下内容。 也可以在bottomsheet的rootview里试试(我没在root view里试过)

android:background="@android:color/white"
android:elevation="16dp"

如果没有背景则可以使用

android:outlineProvider="bounds"

例如,我的工作表位于嵌套滚动视图中

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
  app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        android:elevation="16dp"
        android:outlineProvider="bounds"
        >

    <include layout="@layout/bottomsheet_1" />

    </android.support.v4.widget.NestedScrollView>

诀窍是使用CardView作为父级并在CardView设置高程

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:background="#fff"
    android:clickable="true"
    android:focusable="true"
    app:behavior_hideable="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:layout_height="140dp"
    app:cardElevation="8sp"
    card_view:cardCornerRadius="0dp">

      <!--The content of your Bottom sheet-->
        <android.support.constraint.ConstraintLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
          .
          .
      </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>

编辑

如果您支持 Kitkat 及以下版本,则此技术不是最佳解决方案。 这是由于 Cardview 添加了额外的边距。

我想这会帮助你

首先创建像波纹管一样的底部工作表,然后包含在您的主要活动中

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/bottom_sheet"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 app:behavior_hideable="true"
 app:behavior_peekHeight="56dp"
 android:layout_marginTop="0.5dp" // this margin depend on shadow area
 android:background="set you color"
 android:elevation="20dp" // chose your custom elevation 
 app:layout_behavior="@string/bottom_sheet_behavior">

    <LinearLayout
     android:layout_marginTop="1dp" // this margin depend on max elevation
     android:layout_width="match_parent"
     android:layout_height="200dp">

   </LinearLayout>

</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM