Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中利用 shape 定義控件外觀的用法

編輯:關於 Android 程式設計

在 Android 中,我們常透過 shape 來定義控件的外觀屬性。以下整理了 shape 的幾個常用標籤與屬性。

先看下面的程式碼:

 <shape> <!-- 實心 --> <solid android:color="#ff9d77"/> <!-- 漸變 --> <gradient android:startColor="#ff8c00" android:endColor="#FFFFFF" android:angle="270" /> <!-- 描邊 --> <stroke android:width="2dp" android:color="#dcdcdc" /> <!-- 圓角 --> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape>

solid:實心,也就是填充的意思,android:color 用來指定填充的顏色。

gradient:漸變。android:startColor 和 android:endColor 分別為起始與結束顏色,android:angle 是漸變角度,必須為 45 的整數倍。此外,漸變預設模式為 android:type="linear"(線性漸變),也可指定為徑向漸變 android:type="radial",徑向漸變還需要指定半徑 android:gradientRadius="50"。

stroke:描邊

android:width="2dp" 為描邊寬度,android:color 為描邊顏色。我們還可以把描邊做成虛線,設定方式為 android:dashWidth="5dp" 與 android:dashGap="3dp",其中 dashWidth 表示一段橫線的寬度,dashGap 表示間隔的距離。

corners:圓角

android:radius 為角的弧度,值越大角就越圓。我們也可以把四個角設成不同角度:

 <corners android:topRightRadius="20dp" 右上角 android:bottomLeftRadius="20dp" 左下角 android:topLeftRadius="1dp" 左上角 android:bottomRightRadius="0dp" 右下角 />

這裡有個地方要注意:bottomLeftRadius 對應的是右下角而非左下角,這一點有點反直覺,用時別搞錯就好。另外網上有人說設成 0dp 無效,但我在測試中發現是可以的,或許是版本已修正了這個問題;若真的無效,那就退而設成 1dp。

padding:間隔

這個就不用多說了,XML 佈局檔中經常用到。

大體上就是這樣,以下是一個具體的使用示例:把 shape 用在 Selector 中作為 Button 的背景,分別定義按鈕的一般狀態、獲得焦點狀態與按下時的狀態。

main.xml:

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

運行效果如下圖:

一般狀態:

獲得焦點狀態:

按下狀態:

以上所述就是本文分享的 Android 中 shape 定義控件外觀的用法,希望大家喜歡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved