Android Shape屬性大全

在Android開發中我們爲了美觀很多UI設計都需要一些背景圖片,爲了能減少資源圖片,減小apk的大小,以及更好的適配(當然.9圖片也可以很好的適配),Android爲我們提供了shape可以定義一定規則的形狀,如矩形(rectangle),橢圓形(oval),線性(line)和環形(ring)。

幾個基本屬性:

solid填充色

<solid android:color="#2196F3"/>

corners圓角

<corners android:radius="10dp"//全部圓角的半徑,無限大就成半圓如9999dp
        android:topLeftRadius="5dp"//左上角圓半徑
        android:topRightRadius="5dp"//右上角圓半徑
        android:bottomLeftRadius="5dp"//左下角圓半徑
        android:bottomRightRadius="5dp" />右下角圓半徑

stroke邊框

<stroke
        android:width="1px"//邊框的寬度
        android:color="#ff0000"//邊框的顏色
        android:dashWidth="1dp"//虛線的寬度,值爲0時是實線 
        android:dashGap="1dp"/>//虛線的間隔

size大小

<size
        android:width="30dp"
        android:height="15dp" />

gradient漸變色

<gradient
        android:type="linear"//linear線性漸變(默認) ,radial放射漸變, sweep掃描式漸變
        android:startColor="#B3CDCE"//漸變開始點的顏色
        android:centerColor="#673AB7"//漸變中間點的顏色,在開始與結束點之間
        android:endColor="#074747"//漸變結束點的顏色
        android:angle="90"//漸變角度,必須爲45的倍數,0爲從左到右,90爲從上到下
        android:centerX="0.5"//漸變中心X的相當位置,範圍爲0~1
        android:centerY="0.5"//漸變中心Y的相當位置,範圍爲0~1
        android:gradientRadius="5dp"//漸變的半徑,只有當漸變類型爲radial時才能使用
        android:useLevel="false" />//使用LevelListDrawable時就要設置爲true,設爲false時纔有漸變效果

padding內邊距

<padding
        android:left="10dp"//左內邊距
        android:top="10dp"//上內邊距
        android:right="10dp"//右內邊距
        android:bottom="10dp" />//下內邊距

最好把這些屬性組合成一個例子:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid android:color="#2196F3"/>
    <corners android:radius="9999dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp" />
        />
    <stroke
        android:width="1px"
        android:color="#ff0000"
        android:dashWidth="1dp"
        android:dashGap="1dp"/>
    <size
        android:width="30dp"
        android:height="15dp" />
    <gradient
        android:type="linear"
        android:angle="90"
        android:centerX="0.5"
        android:centerY="0.5"
        android:startColor="#B3CDCE"
        android:centerColor="#673AB7"
        android:endColor="#074747"
        android:gradientRadius="5dp"
        android:useLevel="false" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>

更多請關注公衆號:先跑的笨鳥

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章