Android之shape属性详解

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/494148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

S3C6410移植u-boot-2010.3(2)基本的启动信息修改

1、启动模块修改 进入/cpu/arm1176/目录&#xff0c;修改start.S文件 首先找到需要修改的CONFIG_NAND_SPL汇编原码&#xff0c;修改如下&#xff1a; #ifndef CONFIG_NAND_SPL /** flush v4 I/D caches*/ mov r0, #0 mcr p15, 0, r0, c7, c7, 0 /* flush v3/v4 cache */ mcr p1…

[修订版]”大脑“爆发背后是50年互联网架构重大变革

前言&#xff1a;面对即将到来的2019年&#xff0c;互联网诞生50年将是诸多纪念活动中重要的一个&#xff0c;经过50年的发展&#xff0c;互联网究竟发生什么重要的变化&#xff0c;通过这篇文章试图进行一次总结&#xff0c;也提前向互联网50年致敬。作者&#xff1a;刘锋 互…

Android之自定义ViewGroup

概述 在写代码之前&#xff0c;我必须得问几个问题&#xff1a; 1、ViewGroup的职责是啥&#xff1f; ViewGroup相当于一个放置View的容器&#xff0c;并且我们在写布局xml的时候&#xff0c;会告诉容器&#xff08;凡是以layout为开头的属性&#xff0c;都是为用于告诉容器…

C# 5.0 CallerMemberName CallerFilePath CallerLineNumber 在.NET4中的使用

C# 5.0 给我们带来了三个非常有用的编译器特性 CallerMemberName CallerFilePath CallerLineNumber 在C与C中由下列字符帮助我们实现调试消息的文件行号 01.#define debug_msg printf("%s[%d]:",__FILE__,__LINE__);printf 在.NET 4中与其功能相等的是 new StackTrac…

智慧城市建设:科技创业的下一个浪潮

来源&#xff1a;资本实验室随着全球城市化进程的加速&#xff0c;越来越多的人涌进城市&#xff0c;这为城市建设带来了一系列的挑战&#xff1a;一方面&#xff0c;城市需要面对大量的越来越老化的基础设施&#xff1b;另一方面&#xff0c;需要为新涌入的城市居民提供新的&a…

Android之ViewDragHelper

在自定义ViewGroup中&#xff0c;很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等)&#xff0c;针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是一件很不容易的事&#xff0c;需要自己去处理&#xff1a;多手指的处理、加速度检测等等。 好…

DARPA人工智能技术研究情况一览

来源&#xff1a;一体化指挥调度国家工程实验室、高端装备发展研究中心摘要&#xff1a;20世纪60年代初&#xff0c;DARPA&#xff08;当时为ARPA&#xff09;开始介入自主技术研究&#xff0c;并很快成为该领域的主要研究机构。DARPA意识到&#xff0c;人工智能可以满足大量的…

ListView滑动删除效果实现

通过继承ListView然后结合PopupWindow实现 首先是布局文件&#xff1a; delete_btn.xml&#xff1a;这里只需要一个Button <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/a…

直接读取硬盘扇区

Linux系统下一切都是文件&#xff0c;可以像使用普通文件一样使用设备&#xff0c;可直接操作设备扇区内容,这种方式不经过文件系统。 #include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#in…

深入“肠-脑”神经高速通道,揭开“第六感觉”面纱

来源&#xff1a;中国生物技术网直觉是什么&#xff1f;通常被描述为超感官的第六感觉&#xff0c;它在英文里直译就是肠道感觉。肠道作为“第二大脑”的事实已经家喻户晓了。如果你曾在重要的演讲前感到心慌恶心&#xff0c;或者在一顿大餐后感到头昏眼花&#xff0c;那就是肠…

移动传感器扫描覆盖

移动传感器扫描覆盖摘要&#xff1a;关于传感器网络中的地址覆盖问题&#xff0c;已经做过很多尝试。他们通常归为两类&#xff0c;全覆盖和栅栏覆盖&#xff0c;统称为静态覆盖。在这篇论文中&#xff0c;我们研究一种新的覆盖方案&#xff0c;扫描覆盖&#xff0c;一种不同于…

Andoird自定义ViewGroup实现竖向引导界面

一般进入APP都有欢迎界面&#xff0c;基本都是水平滚动的&#xff0c;今天和大家分享一个垂直滚动的例子。 先来看看效果把&#xff1a; 首先是布局文件&#xff1a; <com.example.verticallinearlayout.VerticalLinearLayout xmlns:android"http://schemas.android.…

科技|全球首款飞行汽车开始量产!下月开始预售,2023年后或可实现一键打“飞车”...

来源&#xff1a; 世界科技创新论坛飞机与汽车结合的产物真的要来了。在2018全球未来出行大会上&#xff0c;吉利副总裁杨学良透露&#xff0c;由吉利控股的全资子公司、全球首家飞行汽车公司美国太力飞行汽车公司推出的“全球首款量产飞行汽车”——Transition&#xff0c;将于…

Android手势锁实现

最终效果如下 整体思路 a、自定义了一个RelativeLayout(GestureLockViewGroup)在里面会根据传入的每行的个数&#xff0c;生成多个GestureLockView&#xff08;就是上面一个个小圈圈&#xff09;&#xff0c;然后会自动进行布局&#xff0c;里面的宽度&#xff0c;间距&#x…

quartz详解

Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目&#xff0c;它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个&#xff0c;百个&#xff0c;甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或 EJBs。…

智能连接:5G与人工智能、物联网等技术的超级融合

来源&#xff1a;资本实验室随着新技术的成熟&#xff0c;新型的、先进的应用将来自5G、人工智能&#xff08;AI&#xff09;和物联网&#xff08;IoT&#xff09;的融合。这种融合将创造出一个智能连接的世界&#xff0c;对所有个人、行业、社会和经济产生积极影响。从现在到2…

一个绚丽的loading动效分析与实现!

最终效果如下 从效果上看&#xff0c;我们需要考虑以下几个问题&#xff1a; 1.叶子的随机产生&#xff1b; 2.叶子随着一条正余弦曲线移动&#xff1b; 3.叶子在移动的时候旋转&#xff0c;旋转方向随机&#xff0c;正时针或逆时针&#xff1b; 4.叶子遇到进度条&#xff…

SQL Server中行列转换 Pivot UnPivot (转载)

SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名&#xff08;即行转列&#xff09;&#xff0c;在SQL Server 2000可以用聚合函数配合CASE语句实现PIVOT的一般语法是&#xff1a;PIVOT(聚合函数(列) FOR 列 in (…) )AS P 完整语法&#xff1a; table_source PI…

20岁的谷歌,和它“最成功”的大败笔

来源&#xff1a;大数据文摘编译&#xff1a;张驰、JIN、涂世文、钱天培谷歌20岁了&#xff01;20年中&#xff0c;谷歌打造了无数或成功或流产的产品&#xff0c;其中&#xff0c;这一名为“谷歌光纤”计划的失败或许是它最“成功”的“大败笔”。2010年&#xff0c;谷歌宣布了…

什么是REST?

REST是Representational State Transfer的简称&#xff0c;表征状态转移。它是一种设计风格。 维基上对其风格的表述为&#xff1a; 资源是由URI来指定。对资源的操作包括获取、创建、修改和删除资源&#xff0c;这些操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法。通…