Java Android:导航抽屉垂直阴影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23690162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:43:28  来源:igfitidea点击:

Android: Navigation Drawer vertical shadow

javaandroidnavigation-drawershadow

提问by Aspicas

I have implemented a NavigationDrawerin my application. I would like to know how I can add a vertical shadow effect which is below the main Fragment, similar to the picture below.

我已经NavigationDrawer在我的应用程序中实现了。我想知道如何在主 Fragment 下方添加垂直阴影效果,类似于下图。

enter image description here

在此处输入图片说明

I have one image on my drawable with the shadow image. It's called "drawer_shadow.9" but I don't know how I can implement this inside my NavigationDrawer.

我的drawable上有一张带有阴影图像的图像。它被称为“drawer_shadow.9”,但我不知道如何在我的NavigationDrawer.

采纳答案by user2511882

You will need to use a drawable for the shadow. Use the setDrawerShadowmethod on the navigationDrawer object. For example:

您将需要为阴影使用可绘制对象。setDrawerShadow在 navigationDrawer 对象上使用该方法。例如:

navigationDrawer.setDrawerShadow(R.drawable.someDrawable, GravityCompat.START);

Link to the official document: setDrawerShadow

官方文档链接:setDrawerShadow

Hope this helps

希望这可以帮助

回答by Krzysztof Dziuba

(Drawer on right side) Activity layout:

(右侧抽屉)活动布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    ...
</LinearLayout>

<LinearLayout
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:orientation="horizontal">

    <View
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:background="@drawable/drawer_shadow"
        android:paddingEnd="0dp"
        android:paddingLeft="-20dp"
        android:paddingRight="0dp"
        android:paddingStart="-20dp" />

    <fragment
        android:id="@+id/fragment_drawer"
        android:name="com.....HomeDrawer"
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:choiceMode="singleChoice"
        tools:layout="@layout/drawer_home" />

</LinearLayout></android.support.v4.widget.DrawerLayout>

draver shadow:

抽屉阴影:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <gradient
            android:startColor="#00FFFFFF"
            android:endColor="#f2e9e9e9"
            android:type="linear" />
        <size
            android:height="@dimen/activity_vertical_margin"
            android:width="5dp">
        </size>
    </shape>
</item>

effect:

影响:

enter image description here

在此处输入图片说明