Android之shape属性详解

摘要:
--描边--˃shape做虚线拿shape做虚线,shape设置为line,stroke是描边属性,其中dashGapdashWidth两个属性彼此一起存在才生效。dashGap:两段之间的空隙宽度、dashWidth:一段线的宽度效果如下shape做渐变实线gradient表示渐变angle渐变角度,45的倍数。startColorendColorcenterColor起止中的颜色效果如下shape做view背景选择器这里注意,item的state_pressed=true是选择状态,按下,另一个不设置就是正常状态。--shape:oval椭圆rectangle:方形line:线性--˃˂!

有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background=”@drawable/shape”, 定义的shape 文件,放在 res/shape 目录下

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

先看我们用shape 都可以做什么

shape下面 一共有6个子节点, 常用的 就只有 四个,padding 和size 一般用不到。

  • corners ———-圆角
  • gradient ———-渐变
  • padding ———-内容离边界距离
  • size ————大小 
  • solid  ———-填充颜色
  • stroke ———-描边
<?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>

shape 做虚线

拿shape 做虚线,shape 设置为line , stroke 是描边属性,
其中 dashGap dashWidth 两个属性彼此一起存在才生效。

dashGap :两段之间的空隙宽度、
dashWidth :一段线的宽度

<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>

效果如下

Android之shape属性详解第1张

shape做渐变实线

gradient 表示渐变
angle 渐变角度,45的倍数。
startColor endColor centerColor 起 止 中 的颜色

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

    <gradient
        android:type="linear"
        android:angle="0"
        android:endColor="#F028A2"
        android:startColor="#2A99F3" />
</shape>

效果如下

Android之shape属性详解第2张

shape 做view背景选择器

这里注意 ,item 的 state_pressed=true 是选择状态,按下,另一个不设置 就是 正常状态。
solid :是填充颜色
corners:设置 四个角的弧度

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

            <!--填充色  -->
            <solid 
                android:color="#ffffff" />  

            <!-- 描边 -->  
            <!-- dashGap:- 与- 虚线之间的间隔  dashWidth: 实线的宽度width: color:-->
         <!--       <stroke              
                android:dashGap="10dp"
                android:dashWidth="5dp"
                android: 
                android:color="#d3d3d3"
                /> -->
            <!-- 圆角 -->  
            <corners
                android:topRightRadius="10dp"     
                android:bottomLeftRadius="10dp"   
                android:topLeftRadius="10dp"    
                android:bottomRightRadius="10dp"    
                />
        </shape>
    </item>
    <item >
        <!--shape:oval 椭圆     rectangle:方形    line:线性-->
        <shape xmlns:android="http://schemas.android.com/apk/res/android"

            android:shape="rectangle">
            <gradient  
                android:startColor="#55B4FE"  
                android:endColor="#3d8FFB" 
                android:type="linear"/>
            <!-- 描边 -->  
         <!--       <stroke 
               android:dashGap="10dp"
                android:dashWidth="5dp"
                android: 
                android:color="#d3d3d3"
                /> -->
            <!-- 圆角  上下左右四个角 弧度-->  
            <corners
                android:topRightRadius="10dp"     
                android:bottomLeftRadius="10dp"   
                android:topLeftRadius="10dp"    
                android:bottomRightRadius="10dp"    
                />   
        </shape>
    </item>

</selector>

效果如下

Android之shape属性详解第3张

shape 做矩形

android:shape=”rectangle”选为矩形

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



    <!-- 渐变 -->
    <gradient
        android:type="linear"
        android:startColor="@android:color/holo_blue_bright"
        android:centerColor="@android:color/holo_green_dark"
        android:endColor="@android:color/holo_red_light"
        android:useLevel="true"
        android:angle="45"/>




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

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

</shape>

效果如下

Android之shape属性详解第4张

shape 作描边矩形 和 椭圆

shape 作描边矩形 和 椭圆
这里注意shape
android:shape=”oval” 椭圆

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


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

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

    <corners 
      android:topLeftRadius="20dp"            
android:topRightRadius="20dp"           
android:bottomLeftRadius="20dp"     
android:bottomRightRadius="20dp" 
        android:radius="50dp"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">


    <!-- 填充 -->
    <solid

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

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

</shape>

效果如下

Android之shape属性详解第5张

代码

ShapeDemo代码

参考链接

*Android shape属性整理 - Because if I don’t write it down, I’ll forget it - 博客频道 - CSDN.NET

免责声明:文章转载自《Android之shape属性详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping微信公众平台的开发过程及其要点下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android 开发 VectorDrawable 矢量图 (八)animation-list帧动画配合矢量图实现动画

前言  只是矢量图的一个使用小技巧,关键点是<aapt:attr name="android:drawable"> 属性,它其实是代替<item 里的android:drawable属性。理解它,你可以举一反三使用到更多的需要实现动画,按下效果,选择效果的xml文件上,比如一个实现按下效果的xml将原来需要3个xml文件合并成一个xml文...

检测当前运行环境——移动端与PC端。

方法1: $(function checkBrowser(){ var browser={      versions:function(){             var u = navigator.userAgent, app = navigator.appVersion; return { //移动终端浏览器版本信息                ...

Android四大组件--Broadcast Receiver具体解释

本文主要讲述了: 一、BroadcastReceiver概述: 二、BroadcastReceiver事件分类 三、BroadcastReceiver事件的编程流程 四、两类BroadcastReceiver 五、普通广播和有序广播 六、Service与BroadcastReceiver怎样交互? 七、开机自己主动执行service 八、Broadcas...

辅助模式最终考验的是想象力,先来看看怎么用!| Accessibility

一、序 Hi,大家好,我是承香墨影! Android 的辅助模式(Accessibility)功能非常的强大。基本上被获取到授权之后,可以监听手机上的任何事件,例如:屏幕点击、窗口的变化、以及模拟点击、模拟系统按键等等。 比较常见的实际使用例子,就是一般应用市场,会推荐开启辅助模式,以便在安装 Apk 的时候,自动帮你点击“下一步”和“安装”按钮。还有个...

Android环境配置和移动自动化(Genymotion)相关配置

本机环境: Window10 其他材料准备: 安卓开发者网站(AndroidStudio下载-3.6.3) 安卓开发工具网站(SDKTools下载-android-sdk_r24.4.1-windows.zip) Genymotion模拟器下载(如果未安装VirtualBox 虚拟机, 选择with VirtualBox-genymotion-3.1.0...

Android网页浏览器的开发

Android网页浏览器的核心Widget是包含了WebKit的WebView。 首先,布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and...