Android: 如何使用shape.xml显示类似阴影的渐变效果

9

你好,我正在尝试使用shape.xml只在顶部获得类似渐变的阴影。

进入图片描述

从图片中可以看到,我将按钮包裹在线性布局中,并希望在布局顶部设置渐变。

我尝试了以下代码,但没有效果:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#f0f0f0" />
            <solid android:color="#f0f0f0" />

        </shape>
   </item>

   <item android:top="1dp"> 
      <shape 
        android:shape="line">
       <gradient

        android:angle="90"
        android:type="linear"
        android:startColor="#FFFFFF"
        android:endColor="#000000" />
        </shape>
   </item>

</layer-list>
2个回答

18

不必使用Layer-list,你可以通过Gradient实现。尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#b4909090" android:endColor="#b4fafafa" android:angle="90"/>
</shape>

根据您的需求更改startColorendColor。结果如下 ->

Image For Shadow


2
那段代码给整个线性布局添加了渐变效果..只想要一条线添加到布局上。 - hari86
您没有说明如何使用它。如果您在视图上方有一个 ImageView 并将其设置为 ImageView 的背景,则此操作是有效的。当您将其作为 LinearLayout 的背景应用时,整个视图都将具有渐变背景。他希望他的背景是白色的,并且顶部有渐变边框。 - Gokhan Arik

2

只需使用以下代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
  android:type="linear"
  android:centerX="78%" 
  android:startColor="#FFf7f7f7" 
  android:centerColor="#FFC0C0C0" 
  android:endColor="#FFbebebe" 
  android:angle="90"/>
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接