Android视图阴影

我search了四周,我找不到一个正确的方法来做到这一点。 我想对我的观点产生以下阴影效果: 在这里输入图像描述

在这里输入图像描述

说实话,我不知道这第二个是否是通过应用阴影效果来完成的。 有任何想法吗?

我知道这个问题已经被回答了,但是我想让你知道我在Android Studio上发现了一个与你在问题中的图片非常相似的drawable :看看这个:

 android:background="@drawable/abc_menu_dropdown_panel_holo_light" 

它看起来像这样:

在这里输入图像描述

希望这会有帮助

编辑

以上选项适用于旧版Android Studio因此您可能无法find它。 对于更新的版本:

 android:background="@android:drawable/dialog_holo_light_frame" 

而且,如果你想拥有自己的自定义形状,我build议使用一个像Photoshop这样的绘图软件来绘制它。

在这里输入图像描述

不要忘记保存为.9.png文件(例如: my_background.9.png

阅读文档: 绘制9修补程序

编辑2

一个更好,更难的工作解决scheme是使用CardView并设置app:cardPreventCornerOverlap="false"以防止意见重叠的边界:

 <android.support.v7.widget.CardView android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="2dp" app:cardElevation="2dp" app:cardPreventCornerOverlap="false" app:contentPadding="0dp"> <!-- your layout stuff here --> </android.support.v7.widget.CardView> 

还要确保在build.gradle包含了最新版本,现在是

 compile 'com.android.support:cardview-v7:26.0.0' 

我正在使用Android Studio 0.8.6,但无法find:

 android:background="@drawable/abc_menu_dropdown_panel_holo_light" 

所以我find了这个:

 android:background="@android:drawable/dialog_holo_light_frame" 

它看起来像这样:

在这里输入图像描述

使用以下代码在res / drawable文件夹中创buildcard_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"/> 

CardView在android 5+中给你真正的影子,它有一个支持库。 只要用它来包装你的观点,你就完成了。

 <android.support.v7.widget.CardView> <MyLayout> </android.support.v7.widget.CardView> 

它需要下一个依赖。

 dependencies { ... compile 'com.android.support:cardview-v7:21.0.+' } 

放置@android:drawable/dialog_holo_light_frame的背景,给出阴影,但不能改变背景颜色和边框样式,所以最好从它的阴影中获益,同时仍然可以通过图层列表放置背景

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--the shadow comes from here--> <item android:bottom="0dp" android:drawable="@android:drawable/dialog_holo_light_frame" android:left="0dp" android:right="0dp" android:top="0dp"> </item> <item android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"> <!--whatever you want in the background, here i preferred solid white --> <shape android:shape="rectangle"> <solid android:color="@android:color/white" /> </shape> </item> </layer-list> 

把它保存在shadow.xml文件夹下的drawable文件夹中

把它分配给一个视图,在xml布局文件中设置它的背景

 android:background="@drawable/shadow" 

使用高程属性来实现阴影效果:

 <View ... android:elevation="2dp"/> 

这只是通过v21使用,看看这个链接: http : //developer.android.com/training/material/shadows-clipping.html

这可能会迟到,但对于那些仍在寻找答案的人,我发现了一个关于git hub的项目,这是唯一真正符合我需求的项目。 Android的materialshadowninepatch

只需在build.gradle依赖项上添加这一行即可

 compile 'com.h6ah4i.android.materialshadowninepatch:materialshadowninepatch:0.6.3' 

干杯。 竖起大拇指为创造者! happycodings