ShapeDrawable资源

Demo

MainActivity.java

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/btn"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:background="@drawable/btn_bg"
        android:textColor="#fff"
        android:text="点击" />
</RelativeLayout>

res/drawable/btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="10dp"></corners>
    <solid
        android:color="#07B226"></solid>
    <size
        android:height="50dp"
        android:width="100dp"></size>
</shape>

运行

这里写图片描述

Button的背景变成了带圆角的长方形。

ShapeDrawable资源

资源路径

res/drawable/filename.xml

资源的使用

在代码中使用:R.drawable.filename

在XML中使用:@[package:]drawable/filename


语法

<?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="float"
        android:centerY="float"
        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>
根标签。

属性

android:shape
值为关键字,表示不同的形状。

关键字 关键字描述
“Computer” 生成的视图为矩形,不设置android:shape属性默认为矩形
“oval” 生成的视图为椭圆形状
“line” 生成的视图为线性,需要<stroke>标签定义线宽。
“ring” 生成的视图为环形

(仅当 android:shape=”ring” 时才使用以下属性)

android:innerRadius
值为尺寸,环内部(中间的孔)的半径,以尺寸值或尺寸资源表示。

android:innerRadiusRatio
值为浮点型,环内部的半径,以环宽度的比率表示。

android:thickness
值为浮点型。环的厚度,表示为环宽度的比率。例如,如果 android:thicknessRatio=”2”,则厚度等于环宽度除以 2。此值被 android:innerRadius 覆盖。默认值为 3。

android:useLevel
值为布尔值,如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。


<corners>
为形状产生圆角。仅当形状为矩形时适用。

属性

android:radius
值为尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。

android:topLeftRadius
值为尺寸。左上角的半径,以尺寸值或尺寸资源表示。

android:topRightRadius
值为尺寸。右上角的半径,以尺寸值或尺寸资源表示。

android:bottomLeftRadius
值为尺寸。左下角的半径,以尺寸值或尺寸资源表示。

android:bottomRightRadius
值为尺寸。右下角的半径,以尺寸值或尺寸资源表示。


<gradient>
指定形状的渐变颜色。

属性
android:angle
值为整型。渐变的角度(度)。0 为从左到右,90 为从上到上。必须是 45 的倍数。默认值为 0。

android:centerX
值为浮点型。渐变中心的相对 X 轴位置 (0 - 1.0)。

android:centerY
值为浮点型。渐变中心的相对 Y 轴位置 (0 - 1.0)。

android:centerColor
值为颜色。起始颜色与结束颜色之间的可选颜色,以十六进制值或颜色资源表示。

android:endColor
值为颜色。结束颜色,表示为十六进制值或颜色资源。

android:gradientRadius
值为浮点型。渐变的半径。仅在 android:type=”radial” 时适用。

android:startColor
值为颜色。起始颜色,表示为十六进制值或颜色资源。

android:type
值为关键字。要应用的渐变图案的类型。有效值为:

说明
“linear” 线性渐变。这是默认值。
“radial” 径向渐变。起始颜色为中心颜色。
“sweep” 流线型渐变。

android:useLevel
值为布尔值。如果这用作 LevelListDrawable,则此值为“true”。


<padding>
要应用到包含视图元素的内边距(这会填充视图内容的位置,而非形状)。

属性:

android:left
值为尺寸。左内边距,表示为尺寸值或尺寸资源

android:top
值为尺寸。上内边距,表示为尺寸值或尺寸资源

android:right
值为尺寸。右内边距,表示为尺寸值或尺寸资源

android:bottom
值为尺寸。下内边距,表示为尺寸值或尺寸资源


<size>
形状的大小。

属性

android:height
值为尺寸。形状的高度,表示为尺寸值或尺寸资源

android:width
值为尺寸。形状的宽度,表示为尺寸值或尺寸资源


<solid>
用于填充形状的纯色。

属性

android:color
值为颜色。应用于形状的颜色,以十六进制值或颜色资源表示。


<stroke>
形状的笔划中线。

属性

android:width
值为尺寸。线宽,以尺寸值或尺寸资源表示。

android:color
值为颜色。线的颜色,表示为十六进制值或颜色资源。

android:dashGap
值为尺寸。短划线的间距,以尺寸值或尺寸资源表示。仅在设置了 android:dashWidth 时有效。

android:dashWidth
值为尺寸。每个短划线的大小,以尺寸值或尺寸资源表示。仅在设置了 android:dashGap 时有效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章