有时候 ,为了满足一些需求,我们要用到 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>

效果如下

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>

效果如下

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:width="1dip"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:width="1dip"android:color="#d3d3d3"/> --><!-- 圆角  上下左右四个角 弧度-->  <corners
                android:topRightRadius="10dp"     android:bottomLeftRadius="10dp"   android:topLeftRadius="10dp"    android:bottomRightRadius="10dp"    />   </shape></item></selector>

效果如下

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"/><!-- 填充 -->
<!--     <solidandroid:color="@android:color/white"/>填充的颜色 --><!-- 描边 --><stroke
        android:width="2dp"android:color="@android:color/black"/></shape>

效果如下

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>

效果如下

代码

ShapeDemo代码

参考链接

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

Android之shape属性详解相关推荐

  1. Android中shape属性详解

    一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...

  2. android layout_width 属性,android:layout_weight属性详解

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

  3. Android之EditText属性详解

    一:EditText简介 EditText是一个非常重要的组件,可以说它是用户和Android应用进行数据传输的窗户,有了它就等于有了一扇和Android应用传输的门,通过它用户可以把数据传给Andr ...

  4. shape属性详解使用

    前言:(转载:https://www.cnblogs.com/MianActivity/p/5867776.html) 大神勿喷,只为记录 一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看 ...

  5. android shape大小,Android shape属性详解

    一.概述 最近太忙了,几乎每天都在做项目.同时写两个项目的感觉真爽. 在我们开发中,会经常遇到shape这种属性,这种属性可以在没有美工的情况照样可以实现我们想要的效果.自动动手,丰衣足食. 二.效果 ...

  6. Android之TextView属性详解

    android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) android: ...

  7. Android SystemProperties系统属性详解

    原址:http://blog.csdn.net/wdong_love_cl/article/details/52404692 Systemproperties类在android.os下,但这个类是隐藏 ...

  8. Android输入法window类型,android输入法windowSoftInputMode属性详解章

    android:windowSoftInputMode activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一] ...

  9. Android Build类属性详解

    20220804,截至到现在的所有Build属性 HashMap<String, String> map = new HashMap<>();LogUtils.logError ...

最新文章

  1. MathJax 支持的 Latex 符号总结(各种数学字体)
  2. C#多线程学习5——多线程的自动管理(定时器)
  3. MyBatis架构设计及源代码分析系列(一):MyBatis架构
  4. 树莓派学习笔记 1 -- 硬件的需求以及raspbian系统的安装
  5. IM 推送保障及网络优化详解(三):如何在弱网环境下优化大数据传输?
  6. HDU 4403 A very hard Aoshu problem DFS
  7. JVM垃圾收集器——G1
  8. Java基础中的基础
  9. 超高并发优化技能001--隔离
  10. java8 中的时间和数据的变化
  11. Codeforces Round 258(Div. 2)
  12. dell电脑如何安装ubuntu系统_Dell电脑 U盘启动盘 安装ubuntu
  13. 【渝粤教育】广东开放大学 岭南文化概论 形成性考核 (45)
  14. 答案原文及解释!!守株待兔死脑筋,旁门左道不正规是什么意思指什么含义怎么理解答!!
  15. 可编程直流稳压电源如何保养维护?
  16. nodejs单个暴露,批量暴露
  17. 美洲新世界多开器 NewWorld多开器
  18. 与信号压缩斗到底:光纤VPM技术解4K 60Hz传输架构与带宽之痛
  19. 网站前后端分离情况下如何实现QQ微信等第三方登陆
  20. Luogu P3951 小凯的疑惑【数论】

热门文章

  1. 别再龟速炼丹了!聊聊怎样科学提升训练效率
  2. 清华提出LogME,无需微调就能衡量预训练模型的下游任务表现!
  3. 自训练:超越预训练,展现强大互补特性的上分新范式!
  4. 深入深出Sigmoid与Softmax的血缘关系
  5. Spring Cloud Alibaba基础教程:Sentinel Dashboard中修改规则同步到Apollo
  6. 圆形进度条以及百分率指示器 Scroller类的练习
  7. BPEL4WS基础知识
  8. 论文学习2-Incorporating Graph Attention Mechanism into Knowledge Graph Reasoning Based on Deep Reinforce
  9. 10 操作系统第二章 进程管理 死锁、死锁的处理策略 银行家算法
  10. day21 面向对象之继承和组合