学而实习之 不亦乐乎

Android:shape属性用法简介

2021-07-19 23:09:16

一、简介

在 Android 中很多地方要用到 shape ,用于定义一些背景或者实现背景选择器,其引用方法跟图片一样 ,可以通过设置相应的 View 的 Android:background=”@drawable/shape” 属性来使用;当用在背景选择器中时,可在选择器的item属性中通过 ndroid:drawable="@drawable/shape_select" 引用。

通常使用 shape 做 button 等 View 的背景选择器,也可以做切换tab 时,底部的下划线等。

shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)。

用法如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
</shape>

跟传统的图片相比,shape 标签可以帮助我们有效减小安装包大小,在不同手机的适配上面,shape 标签也表现的更加优秀。

二、相关属性

shape 属性里一共有6个子属性, 常用的只有四个,padding 和size 一般用不到。如下:

1.圆角:corners
2.渐变:gradient
3.内容离边界距离:padding
4.大小:size
5.填充颜色:solid
6.描边:stroke

三、实例

1.shape 基本用法

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
   
    <!--半径,左上角、右上角、左下角、右下角半径-->
    <corners
        android:radius="10dp"
        android:topLeftRadius="2dp"
        android:topRightRadius="2dp"
        android:bottomLeftRadius="2dp"
        android:bottomRightRadius="2dp" />


    <!--开始颜色、中间颜色、结束颜色、
    false有渐变,要使用LevelListDrawable对象,就要设置为true、
    渐变角度(当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍)、
    渐变类型(linear:线性,radial:放射性,sweep:扫描线式)、
    渐变中心X、Y点坐标、
    渐变半径(type="radial"时有效)
    -->
    <gradient
        android:startColor="@android:color/white"
        android:centerColor="@android:color/black"
        android:endColor="@android:color/black"
        android:useLevel="false"
        android:angle="0"
        android:type="radial"
        android:centerX="0"
        android:centerY="0"
        android:gradientRadius="50"/>

    <!--内边距 内容与边距的距离-->
    <padding
        android:top="10dp"
        android:left="10dp"
        android:bottom="10dp"
        android:right="10dp" />

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

    <!--内部填充颜色-->
    <solid
        android:color="@color/colorPrimaryDark"/>

    <!--描边 颜色、宽带、虚线宽度(0为实线)、虚线间隔-->
    <stroke
        android:color="@color/colorPrimaryDark"
        android:width="2dp"
        android:dashWidth="5dp"
        android:dashGap="2dp"/>
</shape>

2.shape 渐变实线

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:type="linear"
        android:angle="0"
        android:endColor="#F028A2"
        android:startColor="#2A99F3" />
</shape>

3.shape 虚线

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke
        android:dashGap="3dp"
        android:dashWidth="8dp"
        android:width="1dp"
        android:color="#009999" />
</shape>

4.实现选择器

在选择器中需要引用已有的 shape 文件,这里不再写出。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_select" android:state_checked="true" />
    <item android:drawable="@drawable/shape_unselect" android:state_checked="false" />
</selector>