您的位置:首页 > 移动开发 > Android开发

初学Android,使用Drawable资源之使用ShapeDrawable资源(十五)

2012-08-03 14:40 363 查看
ShapeDrawable比较简单,用来定义一个基本几何图形,XML的根元素是<shape.../>
下面定义三个shape资源
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="3dip" android:color="#ff0"/>
</shape>
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>
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"/>
    <!-- 设置圆角矩形 -->
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <!-- 设置圆角矩形 -->
    <corners android:radius="8dp"/>
</shape>
主界面的三个EditBox的背景分别使用上前面定义的三个shape
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:background="@drawable/my_shape_1">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:background="@drawable/my_shape_2"/>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:background="@drawable/my_shape_3"/>

</LinearLayout>
运行效果如下



这样的效果看起来怪怪的,文本框的背景被弄的乱七八糟了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: