如何在安卓线性布局上获得阴影?[英] How to get shadow on android linear layout?

本文是小编为大家收集整理的关于如何在安卓线性布局上获得阴影?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

可能的重复:
如何将阴影设置为Android的视图?

在我的Android应用程序中,我需要一个带有圆角的布局,背景应该具有一定的透明度.为此,我正在使用此XML,它可以正常工作:

CustomShape.xml:

<shape android:shape="rectangle"> 
    <gradient android:startColor="#E2FFFFFF" android:endColor="#E2D0E3E5" android:angle="270"/> 
    <corners android:radius="10dp" /> 
</shape>

style.xml:

<style name="LinearLayoutRoundCorners">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/customshape</item>
</style>

我还需要在布局的右侧和底部一个小阴影.我找不到这样做的方法.我已经看到了一个黑客,您将另一个布局放在后面,使其看起来像阴影,但这对我来说是不起作用的,因为我的布局具有透明度,因此看起来不太好.我如何在不使用图像作为布局的背景的情况下在布局上获得漂亮的阴影?

据我所知,

推荐答案

无法将阴影应用于View对象.我建议您看看九个补丁,我用它们做了阴影.

http://developer.android.com/Guide/topics/graphics/2d-graphics.html#nine-patch

(另请参阅此答案带有滴影像)

其他推荐答案

我知道这已经很晚了,但是我正在阅读此问题/答案组合,虽然是不需要图像的替代方法.我在博客上写了一篇文章,解释了.我认为这对未来的访客提供了方便的方便的两个答案.

http://web.archive.org/web/201205202023231/http://www.anotherandroidblog.com/2011/06/29/drop-hadow-linearearout/

本文地址:https://www.itbaoku.cn/post/627378.html

问题描述

Possible Duplicate:
how to set shadow to a View in android?

In my Android app I need a layout with round corners and the background should have some transparency. For this I'm using this xml, it works fine:

customshape.xml:

<shape android:shape="rectangle"> 
    <gradient android:startColor="#E2FFFFFF" android:endColor="#E2D0E3E5" android:angle="270"/> 
    <corners android:radius="10dp" /> 
</shape>

style.xml:

<style name="LinearLayoutRoundCorners">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/customshape</item>
</style>

I also need a small shadow to the right and bottom of the layout. I can not find a way to do this. I've seen a hack where you put another layout behind to make it look like a shadow but that will not work for me as my layout have transparency, so that will not look very good. How can I get a nice looking shadow on my layout without using an image as background of the layout?

推荐答案

As far as I know there is no way to apply a shadow to a View object. I suggest you take a look at nine patches, I have used them to make a shadow.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

(see also this answer Custom ImageView with drop shadow)

其他推荐答案

I know this is way late, but I was reading this question/answer combo and though of an alternative method that doesn't require an image. I threw together a post on my blog explaining. I figure it would be helpful to future visitors to have both of these answers handy here.

http://web.archive.org/web/20120520203231/http://www.anotherandroidblog.com/2011/06/29/drop-shadow-linearlayout/