【问题标题】:Android how to create gradient shadow on left and right of view?Android如何在视图的左右创建渐变阴影?
【发布时间】:2015-12-21 17:35:07
【问题描述】:

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

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

这是我迄今为止尝试过的:

<?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,但它从左到右传播,这不是我想要的。有没有办法为每一侧隔离两个独特的渐变?

这就是我想要做的:

【问题讨论】:

  • 如果答案对你有帮助,你应该接受它是正确的。
  • 找到解决方案了吗?

标签: android xml user-interface gradient shadow


【解决方案1】:

您可以尝试使用包含在可以调整大小的矩形中的gradient:下面的示例是我用于导航抽屉的阴影。您可以对其进行修改以满足您的需要。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#111"
        android:endColor="#00000000">
    </gradient>
    <size
        android:height="@dimen/activity_vertical_margin"
        android:width="5dp">
    </size>
</shape>

或者,您可以使用v7 CardView support library 中的CardView,它支持Android api 7+。

【讨论】:

【解决方案2】:

您需要为它创建自己的可绘制对象。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多