使用(Drawable)资源———ShapeDrawable资源

      ShapeDrawable用于定义一个基本的几何图形(如矩形、圆形、线条等),定义ShapeDrawable的XML文件的根元素是<shape.../>元素,该元素可指定如下属性。

  • android:shape=["rectangel"|"oval"|"line"|"ring"]:指定定义哪种类型的集合图形。       

      定义ShapeDrawable对象的完整语法格式如下:

      

<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:useslevel=["true" |"false"]/>



    <!-- 定义几何形状的内边框 -->

    <padding

        android:left="integer"

        android:top="integer"

        android:right="integer"

        android:bottom="integer"/>

  

    <!-- 定义几何图形的大小 -->

    <size

        android:width="integer"

        android:color="color"

        android:dashWidth="integer"

        android:dashGap="integer"/>



    <!-- 定义使用单种颜色填充 -->

    <solid

        android:color="color"/>



    <!-- 定义为几何图形绘制边框 -->

    <stroke

        android:width="integer"

        android:color="color“

        android:dashWidth="integer"

        android:dashGap="integer"/>

</shape>

下面通过示例来介绍ShapeDrawable资源的定义和使用。

 实例:椭圆形、渐变背景的文本框

      前面介绍TextView时知道该组件可指定一个android:background属性,该属性用于为该文本框指定背景。大部分时候,文本框背景只是一个简单的图片,或者只是一个简单的颜色。

      如果程序使用ShapeDrawable资源作为文本框的android:background属性,则可以在Android应用中做出各种外观的文本框。下面先定义如下的ShapeDrawable资源。

      程序清单:my_shape_1.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <!-- 设置填充颜色 -->
    <solid android:color="#fff"/>
    <!-- 设置四周的内边距 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <!-- 设置边框 -->
    <stroke android:width="3dp" android:color="#ff0"/>
</shape>

接下来定义如下ShapeDrawable资源

       程序清单:my_shape_2.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
 <!-- 定义填充渐变颜色 -->   
    <gradient android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45" />
    <!-- 设置内填充 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <!-- 设置圆角矩形 -->
     <corners android:radius="8dp"/>
</shape>

在定义如下ShapeDrawable资源。

      程序清单:my_shape_3.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <!-- 定义填充渐变颜色 -->
    <gradient android:startColor="#ff0"
        android:endColor="#00f"
        android:angle="45"
        android:type="sweep"/>
    <!-- 设置内填充 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <corners android:radius="8dp"/>

</shape>

定义了上面三个ShapeDrawable资源之后,接下来在界面布局文件中用这三个ShapeDrawable资源作为文本框的背景。界面布局文件代码如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

   <EditText android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/my_shape_1"/>
     <EditText android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/my_shape_2"/>
       <EditText android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/my_shape_3"/>

</LinearLayout>

使用Activity加载、显示上面的界面布局文件,将可以看到如图6.5所示的界面。

 

原文地址:https://www.cnblogs.com/wolipengbo/p/3438891.html