26

我想知道如何在 android 的任何常规视图中添加阴影层。例如:假设我有一个布局 xml,显示类似这样的内容..

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    android:layout_height="wrap_content"  
    android:layout_width="wrap_content"  
    <Button....  
    ...  
</LinearLayout>  

现在,当它显示时,我想在它周围有一个阴影。

4

7 回答 7

38

创建阴影的最佳方法是使用9patch图像作为视图的背景(或ViewGroup包裹视图)。

第一步是创建一个带有阴影的 png 图像。我使用 Photoshop 来创建这样的图像。它真的很简单。

  • 使用 Photoshop 创建新图像。
  • 添加一个图层并创建一个 4x4 的黑色正方形。
  • 通过在图层资源管理器中选择图层并单击标题为 fx 的按钮并选择投影来在图层上创建阴影。
  • 将图像导出为 png。

下一步是从此图像创建 9-patch 可绘制对象。

  • draw9patch从打开android-sdk/tools
  • 打开图片在draw9patch
  • 在正方形的四个边上创建 4 条黑线,如下所示,然后将图像另存为shadow.9.png.

现在您可以将此阴影添加为要添加阴影的视图的背景。添加shadow.9.pngres/drawables. 现在将其添加为背景:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/shadow"
  android:paddingBottom="5px"
  android:paddingLeft="6px"
  android:paddingRight="5px"
  android:paddingTop="5px"
>

我最近写了一篇博文,详细解释了这一点,并包含了我用于创建阴影的 9patch 图像。

于 2013-01-07T12:41:25.357 回答
13

假设您将使用线性布局(我考虑过垂直线性布局)..并且在您的线性布局下方有一个视图。现在为这个视图提供一个开始颜色和结束颜色..我也想得到这个东西,它为我工作..如果您需要更好的效果,那么只需围绕开始和结束颜色进行处理。

活动主

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/vertical"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@drawable/layout_back_bgn"
        android:orientation="vertical" >
    </LinearLayout>

    <View
        android:layout_below="@+id/vertical"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@drawable/shadow"
        >
    </View>


</LinearLayout>

layout_back_bgn.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

<solid android:color="#FF4500" />

</shape>

影子.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">    
    <gradient
        android:startColor="#4D4D4D"
        android:endColor="#E6E6E6"
        android:angle="270"
        >
    </gradient>
</shape>

我尝试发布使用上述代码后拥有的图像,但stackoverflow不允许我因为我没有声誉......对此感到抱歉。

于 2014-12-17T18:50:41.243 回答
10

您可以使用海拔,自 API 级别 21 起可用

由 Z 属性表示的视图的高度决定了其阴影的视觉外观:具有较高 Z 值的视图会投射更大、更柔和的阴影。Z 值较高的视图会遮挡 Z 值较低的视图;但是,视图的 Z 值不会影响视图的大小。要设置视图的高度:

在布局定义中,使用  

android:elevation

 属性。要在活动代码中设置视图的高度,请使用

View.setElevation()

 方法。

来源

于 2016-03-23T16:24:59.163 回答
5

这是我的解决方案的俗气版本...这是对此处找到的解决方案的修改

我不喜欢角落的样子,所以我把它们都褪色了......

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--Layer 0-->
<!--Layer 1-->
<!--Layer 2-->
<!--Layer 3-->
<!--Layer 4 (content background)-->

<!-- dropshadow -->
<item>
    <shape>
        <gradient 
            android:startColor="@color/white"
            android:endColor="@color/white"
            android:centerColor="#10CCCCCC"
            android:angle="180"/>
        <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
    </shape>
</item>

<item>
    <shape>
        <gradient 
            android:startColor="@color/white"
            android:endColor="@color/white"
            android:centerColor="#20CCCCCC"
            android:angle="180"/>
        <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
    </shape>
</item>

<item>
    <shape>
        <gradient 
            android:startColor="@color/white"
            android:endColor="@color/white"
            android:centerColor="#30CCCCCC"
            android:angle="180"/>

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

<item>
    <shape>
        <gradient 
            android:startColor="@color/white"
            android:endColor="@color/white"
            android:centerColor="#40CCCCCC"
            android:angle="180"/>
        <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
    </shape>
</item>

<item>
    <shape>
        <gradient 
            android:startColor="@color/white"
            android:endColor="@color/white"
            android:centerColor="#50CCCCCC"
            android:angle="180"/>
        <padding android:top="0dp" android:right="0dp" android:bottom="2dp" android:left="0dp" />
    </shape>
</item>

<!-- content background -->
<item>
    <shape>
        <solid android:color="@color/PostIt_yellow" />
    </shape>
</item>

于 2013-10-22T22:40:03.707 回答
3

有一个简单的技巧,使用形成阴影的两个视图。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="10dp"
    android:background="#CC55CC">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="0">
            <TableRow>
                <LinearLayout
                    android:id="@+id/content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView  
                        android:layout_width="fill_parent" 
                        android:layout_height="wrap_content"
                        android:background="#FFFFFF" 
                        android:text="@string/hello" />
                </LinearLayout>
                <View
                    android:layout_width="5dp"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="5dp"
                    android:background="#55000000"/>
            </TableRow>
        </TableLayout>
        <View
            android:layout_width="fill_parent"
            android:layout_height="5dp"
            android:layout_marginLeft="5dp"
            android:background="#55000000"/>
    </LinearLayout>
</FrameLayout>

希望这有帮助。

于 2011-02-19T17:10:16.893 回答
0

使用以下代码在 res/drawable 文件夹中创建 card_background.xml:

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

<item>
    <shape android:shape="rectangle">
        <solid android:color="#BDBDBD"/>
        <corners android:radius="5dp"/>
    </shape>
</item>

<item
    android:left="0dp"
    android:right="0dp"
    android:top="0dp"
    android:bottom="2dp">
    <shape android:shape="rectangle">
        <solid android:color="#ffffff"/>
        <corners android:radius="5dp"/>
    </shape>
</item>
</layer-list>

然后将以下代码添加到您想要卡片布局的元素中

android:background="@drawable/card_background"

以下行定义卡片阴影的颜色

<solid android:color="#BDBDBD"/>
于 2021-06-23T16:18:56.727 回答
-1

在此处输入图像描述

</LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="@color/dropShadow" />

在 LinearLayout 下方使用

另一种方法

在此处输入图像描述

在 /drawable 文件夹中创建“rounded_corner_bg.xml”

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

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/primaryColor" />

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

<item
    android:bottom="2dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp">
    <shape android:shape="rectangle">
        <solid android:color="#F7F7F7" />

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

</layer-list>

使用此布局 android:background="@drawable/rounded_corner_bg"

于 2015-08-02T11:14:13.367 回答