Android Shape

本文详细介绍了如何使用XML在Android中绘制各种图形,包括矩形、椭圆、直线和环形等基本形状,以及如何设置圆角、渐变、描边、填充颜色和虚线等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"]      //共有4种类型,矩形(默认)/椭圆形/直线形/环形
    // 以下4个属性只有当类型为环形时才有效
    android:innerRadius="dimension"     //内环半径
    android:innerRadiusRatio="float"    //内环半径相对于环的宽度的比例,比如环的宽度为50,比例为2.5,那么内环半径为20
    android:thickness="dimension"   //环的厚度
    android:thicknessRatio="float"     //环的厚度相对于环的宽度的比例
    android:useLevel="boolean">    //如果当做是LevelListDrawable使用时值为true,否则为false.

    <corners    //定义圆角
        android:radius="dimension"      //全部的圆角半径
        android:topLeftRadius="dimension"   //左上角的圆角半径
        android:topRightRadius="dimension"  //右上角的圆角半径
        android:bottomLeftRadius="dimension"    //左下角的圆角半径
        android:bottomRightRadius="dimension" />    //右下角的圆角半径

    <gradient   //定义渐变效果
        android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变
        android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
        android:centerX="float"     //渐变中心X的相当位置,范围为0~1
        android:centerY="float"     //渐变中心Y的相当位置,范围为0~1
        android:startColor="color"      //渐变开始点的颜色
        android:centerColor="color"     //渐变中间点的颜色,在开始与结束点之间
        android:endColor="color"    //渐变结束点的颜色
        android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用
        android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果

    <padding    //内部边距
        android:left="dimension"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension" />

    <size   //自定义的图形大小
        android:width="dimension"
        android:height="dimension" />

    <solid  //内部填充颜色
        android:color="color" />

    <stroke     //描边
        android:width="dimension"   //描边的宽度
        android:color="color"   //描边的颜色
        // 以下两个属性设置虚线
        android:dashWidth="dimension"   //虚线的宽度,值为0时是实线
        android:dashGap="dimension" />      //虚线的间隔
</shape>


<!-- 连框颜色值 --><item>   
      <shape>   
            <solid android:color="#ff0000" />   
      </shape>   
</item>   
<!-- 主体背景颜色值 -->  
<item android:bottom="3dp" android:right="3dp">   
     <shape>   
           <solid android:color="#ffffff" />  

           <padding android:bottom="10dp"  
                android:left="10dp"  
                android:right="10dp"  
                android:top="10dp" />  
     </shape>       
</item>  
</layer-list> 


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 圆角 -->
    <corners
        android:radius="9dp"
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp"/><!-- 设置圆角半径 -->

    <!-- 渐变 -->
    <gradient
        android:startColor="@android:color/white"
        android:centerColor="@android:color/black"
        android:endColor="@android:color/black"
        android:useLevel="true"
        android:angle="45"
        android:type="radial"
        android:centerX="0"
        android:centerY="0"
        android:gradientRadius="90"/>

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

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

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

    <!-- 描边 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/black"
        android:dashWidth="1dp"
        android:dashGap="2dp"/>

</shape>



<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >   --- 默认为rectangle
    <corners  -- shape=“rectangle”时使用, 
        android:radius="integer"  -- 半径,会被下边的属性覆盖,默认为1dp,
        android:topLeftRadius="integer" 
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient  -- 渐变
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size    -- 指定大小,一般用在imageview配合scaleType属性使用。大小一般会适配滴
        android:width="integer"
        android:height="integer" />
    <solid    -- 填充颜色,可是是十六进制颜色。(比如想设置半透明效果,直接使用十六就只就OK)
        android:color="color" />
    <stroke    -- 指定边框,border,dashWidth和dashGap有一个为0dp则为实线
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"    -- 虚线宽度
        android:dashGap="integer" />    -- 虚线间隔宽度
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"
    ><!-- 其中rectagle表示矩形,oval表示椭圆,line表示水平直线,ring表示环形 -->
    <!-- 节点属性介绍如下 -->
    <corners />
    <!-- 节点1:corners	(圆角)
	    android:radius	圆角的半径 值越大角越圆  
	    android:topLeftRadius	左上圆角半径  
	    android:topRightRadius	右上圆角半径
	    android:bottomLeftRadius	左下圆角半径
	    android:bottomRightRadius	右下圆角半径
    -->
    
    <gradient />
    <!-- 节点2: gradient (背景颜色渐变)
    	android:startColor 起始颜色
		android:centerColor	中间颜色
		android:endColor 末尾颜色
		android:angle 渐变角度,必须为45的整数倍。
		android:type 渐变模式 默认是linear(线性渐变)radial(环形渐变)
		android:centerX X坐标
		android:centerY	Y坐标
		android:gradientRadius radial(环形渐变)时需指定半径
    -->
    
    <padding />
    <!-- 节点3: padding (定义内容离边界的距离)
    	android:left 左部边距
        android:top	顶部边距
        android:right 右部边距
        android:bottom	底部边距
    -->
    
    <size />
    <!-- 节点4:size (大小)
    	android:width 指定宽度
    	android:height 指定高度
     -->
     
    <solid />
    <!-- 节点5:solid (填充) 
    	android:color 	指定填充的颜色
    -->
    
    <stroke />
	<!-- 节点6:stroke (描边) 
		android:width 描边的宽度
        android:color 描边的颜色
       	把描边弄成虚线的形式"- - -":
        android:dashWidth 表示'-'这样一个横线的宽度
        android:dashGap 表示之间隔开的距离
	-->
	
</shape>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值