当前位置:   article > 正文

Android layer-list 圆角阴影背景_android 带阴影的圆角矩形

android 带阴影的圆角矩形

1,首先了解一下layer-list中的shapr:
简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml
  • 1
  • 2
  • 3

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:Android:background="@drawable/文件的名称"
  • 1
  • 2
  • 3

属性:

<shape>  Android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient>  渐变

Android:startColor  

起始颜色

Android:endColor  

结束颜色             

Android:angle  

渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0;

Android:type  

渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  填充

Android:color  

填充的颜色

<stroke >描边

Android:width 

描边的宽度

Android:color 

描边的颜色

Android:dashWidth

 表示'-'横线的宽度

Android:dashGap 

表示'-'横线之间的距离

<corners >圆角

Android:radius  

圆角的半径 值越大角越圆

Android:topRightRadius  

右上圆角半径

Android:bottomLeftRadius 

右下圆角角半径

Android:topLeftRadius 

左上圆角半径

Android:bottomRightRadius 

左下圆角半径

<padding >填充

android:bottom="1.0dip" 

底部填充

android:left="1.0dip" 

左边填充

android:right="1.0dip" 

右边填充

android:top="0.0dip" 

上面填充
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

1,实现圆角阴影的原理为画两个长宽不同,背景色不同的矩形,重叠在一起就实现了阴影效果。
在res/drawable下新建shardow.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- layout background -->
<item>
    <!-- show rectangle area -->
    <shape android:shape="rectangle">
        <gradient
            android:angle="90"
            android:centerColor="#2F000000"
            android:centerX="50%"
            android:centerY="0"
            android:endColor="#1F000000"
            android:startColor="#1F000000" />

        <corners
            android:bottomLeftRadius="6dip"
            android:bottomRightRadius="6dip"
            android:topLeftRadius="6dip"
            android:topRightRadius="6dip" />
    </shape>
</item>
<!-- layout foreground -->
<item
    android:bottom="4dp"
    android:left="3dp"
    android:right="3dp"
    android:top="3dp">
    <!-- show rectangle area -->
    <shape android:shape="rectangle">
        <gradient
            android:angle="270"
            android:endColor="#FFFFFF"
            android:startColor="#FFFFFF" />

        <corners
            android:bottomLeftRadius="4dip"
            android:bottomRightRadius="4dip"
            android:topLeftRadius="4dip"
            android:topRightRadius="4dip" />
    </shape>
</item>
</layer-list>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

在相应的控件中设置background即可:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="-20dp"
            **android:background="@drawable/shadow"**
            android:orientation="vertical"
            android:paddingBottom="15dp"
            android:paddingTop="15dp">
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/420694
推荐阅读
相关标签
  

闽ICP备14008679号