美文网首页
Android-shape属性

Android-shape属性

作者: 沉淀者 | 来源:发表于2020-04-04 11:01 被阅读0次

一、shape属性

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

1)基本属性

Shape可以定义控件的一些展示效果,例如圆角,渐变,填充,描边,大小,边距;shape子标签就可以实现这些效果,shape子标签有下面几个属性:corners,gradient,padding,size,solid,stroke

  • corners(圆角) ----<corners>是用来字义圆角
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <corners    //定义圆角   
    android:radius="10dp"  //全部的圆角半径;   
    android:topLeftRadius="5dp"  //左上角的圆角半径;   
    android:topRightRadius="5dp" //右上角的圆角半径;   
    android:bottomLeftRadius="5dp"  //左下角的圆角半径;   
    android:bottomRightRadius="5dp" /> //右下角的圆角半径。
</shape>    
  • solid(填充色)----<solid>是用以指定内部填充色
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="#ffff00"/> //内部填充色
</shape> 
  • gradient(渐变)-----<gradient>用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <gradient  
    android:type=["linear" | "radial" | "sweep"]   //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变;
    android:angle="90"  //渐变角度,必须为45的倍数,0为从左到右,90为从上到下;   
    android:centerX="0.5"  //渐变中心X的相当位置,范围为0~1;
    android:centerY="0.5"  //渐变中心Y的相当位置,范围为0~1;   
    android:startColor="#24e9f2"  //渐变开始点的颜色;   
    android:centerColor="#2564ef" //渐变中间点的颜色,在开始与结束点之间;   
    android:endColor="#25f1ef"  //渐变结束点的颜色;   
    android:gradientRadius="5dp"  //渐变的半径,只有当渐变类型为radial时才能使用;   
    android:useLevel="false" /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果。  
</shape> 
  • stroke(描边)--<stroke>是描边属性,可以定义描边的宽度,颜色,虚实线等
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <stroke        
    android:width="1dp"   //描边的宽度   
    android:color="#ff0000"   //描边的颜色   
    // 以下两个属性设置虚线   
    android:dashWidth="1dp" //虚线的宽度,值为0时是实线   
    android:dashGap="1dp" />//虚线的间隔
</shape> 
  • padding(内边距)---<padding>是用来定义内部边距
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <padding    
    android:left="10dp"  //左内边距; 
    android:top="10dp"   //上内边距;
    android:right="10dp"   //右内边距;
    android:bottom="10dp" /> //下内边距。
</shape>
  • size(大小)----<size>标签是用来定义图形的大小的
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <size   
    android:width="50dp"  //宽度 
    android:height="50dp" />// 高度
</shape>

2)特殊属性

Shape可以定义当前Shape的形状的,比如矩形,椭圆形,线形和环形;这些都是通过shape标签属性来定义的,shape标签有下面几个属性:rectangle,oval,line,ring

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape=["rectangle" | "oval" | "line" | "ring"] //shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)   
  //下面的属性只有在android:shape="ring"时可用:   
  android:innerRadius="10dp" //  内环的半径; 
  android:innerRadiusRatio="2"  // 浮点型,以环的宽度比率来表示内环的半径;   
  android:thickness="3dp"   // 环的厚度;   
  android:thicknessRatio="2" //  浮点型,以环的宽度比率来表示环的厚度;  
  android:useLevel="false"> //  boolean值,如果当做是LevelListDrawable使用时值为true,否则为false。
</shape>

二. shape用法

1)在res/drawable下新建shape_text.xml文件;

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners  
        android:radius="5dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp" />
     
    <gradient
        android:startColor="#FF0000"
        android:endColor="#80FF00"
        android:angle="45"/> 
        
    <padding 
         android:left="10dp"
         android:top="10dp"
         android:right="10dp"
         android:bottom="10dp"/>
     
    <size
        android:width="200dp"
        android:height="200dp"/>
    
    <solid 
        android:color="#ffff9d"/>
    
    <stroke 
        android:width="2dp"
        android:color="#dcdcdc"/> 
</shape>

2)在布局中引用shape_text.xml文件;

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Shape测试"  
        android:background="@drawable/shape_text"  
        android:textSize="15sp"  
        android:textColor="@android:color/black"/>  
</LinearLayout>

三、Android layer-list基本用法

1.含义

layer-list 作为图层列表,原理是图层一层层的叠加,后添加的会覆盖先添加的,有点类似 RelativeLayout属性。在 layer-list 中可以通过控制后添加图层距离最底部图层的左、上、右、下的四个边距等属性,来得到不同的显示效果。

1)圆环(一个圆环里面包含一个红色的圆)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:dither="true"
            android:shape="oval">
            <solid android:color="#ffffff" />
            <stroke
                android:width="2dp"
                android:color="#ffaa00" />
        </shape>
    </item>

    <item
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp">
        <shape android:shape="oval">
            <solid android:color="#ff0000" />
            <size
                android:width="5dp"
                android:height="5dp" />
        </shape>
    </item>
</layer-list >

相关文章

  • Android-shape属性

    一、shape属性 1.shape属性基本语法: 1)基本属性 Shape可以定义控件的一些展示效果,例如圆角,渐...

  • Android-Shape

    Android-Shape shape一个用来定义形状的工具,或者说使用画图的,图片用于做背景图。 shape定义...

  • Android-Shape标签使用

    前言 工作随笔,方便记起 1.shape的属性 2.shape有6个子标签,各属性如下

  • Android-Shape搞起来A

    大部分初学者做Android项目基本都是搬效果,搬库,各种搬。基本小点的项目,搬搬也就差不多了。所以一段时候,甚至...

  • Android-Shape搞起B_ShapeDrawable

    Go go go....看下官方文档先https://developer.android.google.cn/re...

  • 成员属性、静态属性、私有属性、原型属性

    一、成员属性和成员方法在构造函数中,通过this.属性声明,或者实例化出对象后,通过“对象.属性”追加的,都属于成...

  • swift 属性(存储属性、计算属性、懒加载属性、类型属性)

    存储属性 存储属性:用于存储一个常量或变量 结构体实例赋值给常量,该实例属性不能被修改(因为结构体属于值类型,当值...

  • jQuery属性操作

    attr(属性名,属性值)操作所有属性 removeAttr(属性名) prop(属性名,属性...

  • 依赖属性|简单属性|附加属性

    依赖属性 简单理解就是属性,支持继承,比如 Window 有 Font 属性,Button 也有 Font 属性,...

  • attribpromote

    属性创建。 属性名称,属性类别,默认属性,输出属性。 属性转移,atteibutrename. 在点属性上,现有属...

网友评论

      本文标题:Android-shape属性

      本文链接:https://www.haomeiwen.com/subject/uepdphtx.html