Android中用Shape Drawable實現自訂介面

來源:互聯網
上載者:User

關於Shape使用的官方文檔:

http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

Android中常常使用shape來定義控制項的一些顯示內容,下面是Shape中的所有屬性及一個簡單的樣本:

 

<?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="integer"                android:centerY="integer"                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>

下面是一個小樣本:

main.xml檔案內容如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape>            <!-- 漸層 -->            <gradient android:endColor="#FFFFFF" android:gradientRadius="50" android:startColor="#ff8c00" android:type="radial"/>            <!-- 描邊 -->            <stroke android:dashGap="3dp" android:dashWidth="5dp" android:width="2dp" android:color="#dcdcdc" />            <!-- 圓角 -->            <corners android:radius="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item android:state_focused="true"><shape>            <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />            <stroke android:width="2dp" android:color="#dcdcdc" />            <corners android:radius="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item><shape>            <solid android:color="#ff9d77" />            <stroke android:width="2dp" android:color="#fad3cf" />            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item></selector>

 

button_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>            <!-- 漸層 -->            <gradient android:endColor="#FFFFFF" android:gradientRadius="50" android:startColor="#ff8c00" android:type="radial"/>            <!-- 描邊 -->            <stroke android:dashGap="3dp" android:dashWidth="5dp" android:width="2dp" android:color="#dcdcdc" />            <!-- 圓角 -->            <corners android:radius="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item android:state_focused="true"><shape>            <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />            <stroke android:width="2dp" android:color="#dcdcdc" />            <corners android:radius="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item><shape>            <solid android:color="#ff9d77" />            <stroke android:width="2dp" android:color="#fad3cf" />            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="5dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item></selector>

 

如下:

 一般狀態:

 

按下狀態:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.