Android 如何向底部图纸视图添加阴影?

Android 如何向底部图纸视图添加阴影?,android,android-layout,android-design-library,bottom-sheet,Android,Android Layout,Android Design Library,Bottom Sheet,到目前为止,随着Android设计库中的官方底部表单组件的实现,顶部边缘没有显示阴影。但就我在各种模型和材料设计规范中看到的情况而言,底页包含某种离散阴影 我认为阴影将有助于将底部图纸与主布局隔开,特别是如果设置了peek值和/或底部图纸始终可见。否则,它只会与主布局及其项混合在一起 我尝试了两种方法viewcompt.setElevation(底图,5)并将android:elevation=“5dp”设置为XML中的视图,但未成功 对于API级别21及更高版本,请在父视图中设置以下内容。您也

到目前为止,随着Android设计库中的官方底部表单组件的实现,顶部边缘没有显示阴影。但就我在各种模型和材料设计规范中看到的情况而言,底页包含某种离散阴影

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

我尝试了两种方法
viewcompt.setElevation(底图,5)
并将
android:elevation=“5dp”
设置为XML中的视图,但未成功


对于API级别21及更高版本,请在父视图中设置以下内容。您也可以在底部工作表的根视图中尝试(我还没有在根视图中尝试过)

如果没有背景,那么可以使用

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>

我知道阴影形状与立面的外观不同,但至少可以尝试一下。诀窍是使用
app:layou-anchor
将阴影剪裁到底部纸张上

activity_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>

诀窍是使用
cardwiew
作为父视图,并在
cardwiew

<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>


您使用的是支持库还是什么?@AlexChengalan正确!v4,AppCompat和Design.try
app:cardElevation=“5dp”
但我们这里讨论的是一张底片。我使用线性布局作为工作表的根视图。任何答案都不能解决您的问题,或者您为什么不接受其中一个?我无法让
android:elevation
工作。这是可以接受的。谢谢。谢谢。但是当底部床单被隐藏时,阴影仍然可见。我想它也必须以编程的方式隐藏。你可以在底部工作表的顶部包含阴影,这样当它隐藏时,它就在屏幕外。我喜欢你的方法。感谢您的帮助。我认为需要注意的是,立面主要是在视图下方提供阴影,而不是在视图上方,因此对于像底部板材这样的东西,这很好,它不适用于顶部阴影-仅适用于底部,因为“光”从上角进入(请参见fab shadow)@EVGENIVOOBEI在minSdk 21I上运行得很好,我的
底片上需要一个阴影。由于工作表从底部窥视,默认的
cardwiew
s阴影不够。对我有效的方法是使用
cardwiew
如本文所建议的+改变阴影方向()。
<ShadowView
    android:id="@+id/shadow"
    android:layout_width="match_parent"
    android:layout_height="16dp"
    android:gravity="bottom"
    app:layout_anchor="@id/bottom_sheet" />
<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>
<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>