如何在android中为View设置阴影?
我想知道如何向 android 中的任何常规视图添加阴影层。例如:假设我有一个布局 xml,显示类似这样的内容。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
<Button....
...
</LinearLayout>
现在,当它显示时,我希望在它周围有一个阴影。
I want to know how to add a shadow layer to any general View in android. for eg: suppose i have a layout xml, showing something like this..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
<Button....
...
</LinearLayout>
Now when it is displayed I want to have a shadow around it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
创建阴影的最佳方法是使用
9patch
图像作为视图的背景(或包装视图的
ViewGroup
)。第一步是创建一个周围有阴影的 png 图像。我
使用 Photoshop 创建这样的图像。这真的很简单。
资源管理器并单击标题为 fx 的按钮并选择投影。
下一步是从此图像创建 9 块可绘制对象。
android-sdk/tools
打开draw9patch
draw9patch
中打开图像然后将图像另存为
shadow.9.png
。现在您可以添加此阴影作为您想要的视图的背景
添加阴影。将
shadow.9.png
添加到res/drawables
中。现在添加它作为背景:
我最近写了一篇 博客文章 详细解释了这一点
包括我用来创建阴影的 9patch 图像。
The best way to create a shadow is to use a
9patch
image as thebackground of the view (or a
ViewGroup
that wraps the view).The first step is to create a png image with a shadow around it. I
used photoshop to create such an image. Its really simple.
explorer and clicking on a button titled fx and choosing drop shadow.
The next step is to create 9-patch drawables from this image.
draw9patch
fromandroid-sdk/tools
draw9patch
following and then save the image as
shadow.9.png
.Now you can add this shadow as the background of the views you want to
add the shadow to. Add
shadow.9.png
tores/drawables
. Now add itas a background:
I recently wrote a blog post that explains this in detail and
includes the 9patch image that I use for creating the shadow.
假设您将使用线性布局(我考虑过垂直线性布局)..并且在线性布局下方有一个视图。现在为该视图提供开始颜色和结束颜色..
我也想得到这个东西,它对我有用。如果你需要更好的效果,那么只需解决开始和结束颜色即可。
activity_main
layout_back_bgn.xml
shadow.xml
我尝试发布一张使用上述代码后得到的图像,但 stackoverflow 不允许我这样做,因为我没有声誉..对此感到抱歉。
Assuming u would use a linear layout(i have considered a vertical linear layout)..and have a view just below your linear layout.Now for this view provide a start colour and end colour..
I also wanted to get this thing,its working for me..If you need a even better effect,then just work around the start and end colour.
activity_main
layout_back_bgn.xml
shadow.xml
I tried to post an image which i have it after using the above code,but stackoverflow doesnot allow me coz i dont have reputation..Sorry about that.
您可以使用自 API 级别 21 起可用的海拔
来源
You can use elevation, available since API level 21
Source
这是我的解决方案的俗气版本...这是对找到的解决方案的修改
这里
我不喜欢角落的样子,所以我把它们全部淡化了。 ..
Here's my cheesy version of the solution...This is the modification of the solution found
here
I didn't like how the corners look so I faded all of them...
有一个简单的技巧,使用两个视图形成阴影。
希望这有帮助。
There are a simple trick, using two views that form the shadow.
Hope this help.
使用以下代码在 res/drawable 文件夹中创建 card_background.xml:
然后将以下代码添加到您想要卡片布局的元素中,
以下行定义卡片阴影的颜色
Create card_background.xml in the res/drawable folder with the following code:
Then add the following code to the element to which you want the card layout
the following line defines the color of the shadow for the card
在 LinearLayout 下方使用
另一种方法
在 /drawable 文件夹中创建“rounded_corner_bg.xml”
使用此布局
android:background="@drawable/rounded_corner_bg"
Use Just Below the LinearLayout
Another Method
create "rounded_corner_bg.xml" in /drawable folder
To use this Layout
android:background="@drawable/rounded_corner_bg"