【android】shape的使用

例子:XML 文件保存在 res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <paddingandroid:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp"/>
    <cornersandroid:radius="8dp"/>
</shape>

下面这个 XML 把shape应用到view:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

在程序代码里获取shape,应用到view

Resources res =getResources();
Drawable shape = res.getDrawable(R.drawable.gradient_box);

TextView tv =(TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

 例子

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 渐变 -->
    <!-- 详细内容 http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape -->
    <gradient
        android:startColor="#0000ff"
        android:endColor="#00bfff"
        android:angle="45"
        android:centerX="0.5"/>
    <!-- 圆角 -->
    <corners 
        android:radius="8dp"/>
    <!-- 外边间距 -->
     <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
        <!-- 描边 -->  
            <stroke  
                
                android:width="2dp"  
                android:color="#dcdcdc"  
                android:dashWidth="5dp"  
                android:dashGap="0dp" />
            <!-- dashgap 表示间隔 有了间隔就成了虚线 -->
    
</shape>

 

posted @ 2014-01-06 09:36  江海不系舟  阅读(256)  评论(0编辑  收藏  举报