Shape的相关属性详解

1. gradient渐变

<gradient
  android:angle="integer"
  android:centerX="Float"
  android:centerY="Float"
  android:centerColor="integer"
  android:startColor="color"
  android:endColor="color"
  android:gradientRadius="integer"
  android:type=["linear"|"radial"|"sweep"]
  android:usesLevel=["true"|"false"]
  />
  • angle:角度,当 android:type=“linear”时有效
  • centerX:Float。渐变色中心的 X 相对位置( 0-1.0 )。当 android:type=“linear”时无效
  • centerY:Float。渐变色中心的 Y 相对位置( 0-1.0 )。当 android:type=“linear”时无效
  • centerColor:color。可选的颜色,出现在 start 和 end 颜色之间。
  • gradientRadius:Float。渐变色的半径。当 android:type=“radial” 时有效。
  • startcolor:开始的颜色
  • endcolor:结束的颜色
  • type:Keyword 。渐变色的样式。有效值为:
    “linear”:线性渐变,默认值
    “radial”:环形渐变。 start 颜色是处于中间的颜色
    “sweep”:sweep 渐变
  • useLevel:Boolean。“ true ”表示可以当作 LevelListDrawable 使用

##1.1 android:type=“linear”
例如:在res/drawable下建立一个gradient_shape.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:startColor="#fff"
        android:endColor="#000"
        android:centerColor="#dc143c"
        android:angle="45"/>

</shape>

在res/layout里新建一个activity_gradient.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_shape"
    android:orientation="vertical" >

</LinearLayout>

截图:
这里写图片描述

  • android:startColor="#fff"起始颜色为白色
  • android:endColor="#000"结束颜色为黑色
  • android:centerColor="#dc143c"中间色为红色
  • android:angle="45"渐变角度为45度,从左下到右上

1.2 android:type=“radial”

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        
        android:startColor="#fff"
        android:endColor="#000"
        android:centerColor="#dc143c"
        android:gradientRadius="90"
        android:type="radial"
        />

</shape>

截图:
这里写图片描述

这里颜色渐变的是里向外渐变

  • android:startColor="#fff"起始(最里)颜色为白色
  • android:endColor="#000"结束(最外)颜色为黑色
  • android:centerColor="#dc143c"中间颜色为红色
  • android:gradientRadius="90"渐变半径为90
  • android:type="radial"类型为环形渐变

1.3 android:type=“sweep”

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        
        android:startColor="#fff"
        android:endColor="#000"
        android:centerColor="#dc143c"
        android:centerX="0"
        android:centerY="0"
        android:type="sweep"
        />

</shape>

截图:
这里写图片描述

  • android:startColor="#fff"起始颜色为白色
  • android:endColor="#000"结束颜色为黑色
  • android:centerColor="#dc143c"中间色为红色
  • android:centerX="0"渐变色中心的 X 相对位置( 0-1.0 )。当android:type=“linear”时无效
  • android:centerY="0"渐变色中心的 Y 相对位置( 0-1.0 )。当 android:type=“linear”时无效
  • android:type="sweep"延伸类型

2 . corners 圆角

<corners
  android:radius="integer"
  android:topLeftRadius="integer"
  android:topRightRadius="integer"
  android:bottomLeftRadius="integer"
  android:bottomRightRadius="integer"
  />

corners指的是圆角

  • radius:半径,会被下面的特性覆盖。
  • topLeftRadius:左上圆角半径。
  • topRightRadius:右上圆角半径。
  • bottomLeftRadius:左下圆角半径。
  • bottomRightRadius:右下圆角半径。

3 . padding各个方向的间距

<!-- 间隔 -->
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp"/><!-- 各方向的间隔 -->
        

4 . size尺寸大小

 <!-- 大小 -->
    <size
        android:width="100dp"
        android:height="50dp"/><!-- 宽度和高度 -->

5 . solid背景色填充

<!-- 填充 -->
    <solid
        android:color="@android:color/white"/>

6 . stroke描边

<!-- 描边 -->
     <stroke
        android:width="2dp"
        android:color="#a9a9a9"
        android:dashWidth="2dp"
        android:dashGap="1dp"/>
  • android:width=“2dp” 设置边边的宽度
  • android:color="#a9a9a9" 设置边边的颜色
  • android:dashWidth=“2dp” 设置虚线的宽度
  • android:dashGap=“1dp” 设置虚线的间隔宽度
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值