Android中使用shape来定义控件的显示属性

参见:  http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6087.html  或者

             http://kofi1122.blog.51cto.com/2815761/521605

       Android中,默认的控件显示效果往往不太美观。我们可以在drawable下建一个XML文件,里面放置shape标签以及其相关的属性。例如,可以用这样的方式设置一个LinearLayout的边框、填充颜色、圆角效果等等。

 

例如,在一下代码中(today_but_selector.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <!-- 圆角 -->
            <corners android:radius="5dp" />
            <solid android:color="#888800" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#333333" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:right="10dp" android:bottom="10dp" />
        </shape>
    </item>
</selector>
可以这样利用这个xml文件,例如Button定义为:
<Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="  今 天   "
            android:background="@drawable/today_but_selector" android:textColor="#ffffff" />

得到的按钮显示效果为:

(1)正常情况下:

QQ截图20111012103401

(2)按下的时候:

QQ截图20111012103423

posted @ 2011-10-12 10:36  图形学小菜鸟  阅读(1526)  评论(0编辑  收藏  举报