Android:shape的使用详解(1)
Android:shape的使用详解(2)

在Android开发中,我们经常会觉得我们的界面不好看,今天看到有人说Android学好Shape不再依赖美工,我们先看一些代码,大家应该挺熟悉的

<?xml version="1.0" encoding="utf-8"?>    <shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] >        <corners  android:radius="integer" 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  android:width="integer" android:height="integer" />        <solid  android:color="color" />        <stroke  android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" />    </shape>

在shape这个闭合标签中,有corners、gradient、padding、size、solid、stroke等。接下来我们一个一个讲解。

shape

定义这是一个GradientDrawable,必须作为根元素。
android:shape 
定义shape的值,必须是下面的之一:

  • “rectangle” 矩阵,这也是默认的shape
  • “oval” 椭圆
  • “line” 一条水平的直线。这种shape必须使用 stroke 元素来定义这条线的宽度
  • “ring” 圆环

下面的属性只有当 android:shape=”ring”才使用:

  • android:innerRadius
    尺寸。 内环的半径。一个尺寸值(dip等等)或者一个尺寸资源。
  • android:innerRadiusRatio
    Float类型。这个值表示内部环的比例,例如,如果android:innerRadiusRatio = ” 5 “,那么内部的半径等于环的宽度除以5。这个值会被android:innerRadius重写。 默认值是9。
  • android:thickness
    尺寸。环的厚度,是一个尺寸值或尺寸的资源。
  • android:thicknessRatio
    Float类型。厚度的比例。例如,如果android:thicknessRatio= ” 2 “,然后厚度等于环的宽度除以2。这个值是被android:innerRadius重写, 默认值是3。
  • android:useLevel
    Boolean类型。如果用在 LevelListDrawable里,那么就是true。如果通常不出现则为false

corners

为Shape创建一个圆角,只有shape是rectangle时候才使用。

  • android:radius
    Dimension。圆角的半径。会被下面每个特定的圆角属性重写。
  • android:topLeftRadius
    Dimension。top-left 设置左上角的半径
  • android:topRightRadius
    Dimension。top-right 设置右上角的半径
  • android:bottomLeftRadius
    Dimension。 设置右下角的半径
  • android:bottomRightRadius
    Dimension。设置左下角的半径

gradient

指定这个shape的渐变颜色。

  • android:angle
    Integer。渐变的角度。 0 代表从 left 到 right。90 代表bottom到 top。必须是45的倍数,默认为0
  • android:centerX
    Float。渐变中心的相对X坐标,在0到1.0之间。
  • android:centerY
    Float。渐变中心的相对Y坐标,在0到1.0之间。
  • android:centerColor
    Color。可选的颜色值。基于startColor和endColor之间。
  • android:endColor
    Color。 结束的颜色。
  • android:gradientRadius
    Float 。渐变的半径。只有在 android:type=”radial”才使用
  • android:startColor
    Color。开始的颜色值。
  • android:type
    Keyword。渐变的模式,下面值之一:
    “linear” 线形渐变。这也是默认的模式
    “radial” 辐射渐变。startColor即辐射中心的颜色
    “sweep” 扫描线渐变。
  • android:useLevel
    Boolean。如果在LevelListDrawable中使用,则为true

padding

内容与视图边界的距离

  • android:left Dimension。左边填充距离.
  • android:top Dimension。顶部填充距离.
  • android:right Dimension。右边填充距离.
  • android:bottom Dimension。底部填充距离.

size

这个shape的大小。

  • android:height Dimension。这个shape的高度。
  • android:width Dimension。这个shape的宽度。

    注意:默认情况下,这个shape会缩放到与他所在容器大小成正比。当你在一个ImageView中使用这个shape,你可以使用 android:scaleType=”center”来限制这种缩放。

solid

填充这个shape的纯色

  • android:color Color。颜色值,十六进制数,或者一个Color资源

stroke

这个shape使用的笔画,当android:shape=”line”的时候,必须设置改元素。

  • android:width Dimension。笔画的粗细。
  • android:color Color。笔画的颜色
  • android:dashGap Dimension。每画一条线就间隔多少。只有当android:dashWidth也设置了才有效。
  • android:dashWidth Dimension。每画一条线的长度。只有当 android:dashGap也设置了才有效。

android:dashGap和android:dashWidth设置这条线为虚线的,其中android:dashWidth表示’-‘这样一个横线的宽度,android:dashGap表示之间隔开的距离

更多相关文章

  1. android 各种控件颜色值
  2. Android Webview适配屏幕宽度
  3. 改变进度栏的颜色 progress bar 的背景色
  4. 修改EditText的光标颜色
  5. 关于文字颜色/图片背景---selector状态列表
  6. android 获得屏幕宽度 高度
  7. 3、android颜色取值
  8. Android RGB颜色查询对照表
  9. android中各种颜色在drawable.xml的值

随机推荐

  1. 你为什么得不到梦想中的开发工作?
  2. Go语言的2017年终总结
  3. 从6:1到10:1,技术圈的女程序员都去哪儿了?
  4. 为什么经过三十年的发展,现代计算机的响应
  5. 互联网协议的演变之路
  6. 阿里自研分布式强一致关系型数据库——X-
  7. 所有人都在渲染程序员的中年危机,我们却在
  8. 中国第一份 DevOps 年度调查报告重磅发布
  9. 理性思维VS中二晚期,二次元程序员调查报告
  10. 技术管理从0到1,并没有你想象的那么难!