Android如何在视图的左侧和右侧创建渐变阴影? [英] Android how to create gradient shadow on left and right of view?

查看:281
本文介绍了Android如何在视图的左侧和右侧创建渐变阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Lollipop elevation属性,因为我需要通过API 18定位设备.

I cannot use Lollipop elevation property because I need to target devices from API 18.

我需要在视图的右侧和左侧放置阴影.

I need to place a shadow on the right and left side of a view.

这是我到目前为止尝试过的:

This is what I tried so far :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <item>
        <shape >
            <!-- set the shadow color here -->
            <stroke
                android:width="2dp"
                android:color="#7000" />

            <!-- setting the thickness of shadow (positive value will give shadow on that side) -->

            <padding
                android:bottom="0dp"
                android:left="2dp"
                android:right="2dp"
                android:top="0dp" />

            <corners android:radius="3dp" />
        </shape>
    </item>

    <!-- Background -->

    <item>
        <shape>
            <solid android:color="@color/salmon" />
            <corners android:radius="3dp" />
        </shape>
    </item>
</layer-list>

我尝试在第一项中添加gradients,但是它从左向右扩散,这并不是我所期待的.有没有一种方法可以隔离每侧的两个唯一渐变?

I tried adding gradients in the first item, but it spreads from left to right which is not what I am looking fo. Is there a way to isolate two unique gradients for each side ?

这就是我想要做的:

推荐答案

您需要为其创建自己的可绘制对象.

You need to create your own drawable for it.

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
        <gradient
            android:angle="270"
            android:endColor="@android:color/white"
            android:startColor="@color/black_38">
        </gradient>
        <size

            android:height="4dp">
        </size>
    </shape>

然后例如在相对布局的帮助下与布局的任何一侧对齐.对我来说,这是自定义应用程序栏的底部阴影.

And then for example with help of relative layout to align to any side of your layout. For me it's bottom shadow of custom app bar.

...
    <View
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:background="@drawable/app_bar_shadow"
            android:layout_height="4dp"/> // height of your shadow

这篇关于Android如何在视图的左侧和右侧创建渐变阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆