天天看點

Android shape的屬性和使用詳解

Android Shape的詳解

1.代碼

<?xml version="1.0" encoding="utf-8"?>
<!-- 
shape=["rectangle"(預設) | "oval" | "line" | "ring"]
矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環形(ring)
下面的屬性隻有在android:shape="ring時可用:
innerRadius 尺寸,内環的半徑
innerRadiusRatio 浮點型,以環的寬度比率來表示内環的半徑,
thickness 尺寸,環的厚度
thicknessRatio 浮點型,以環的寬度比率來表示環的厚度
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
	
	<!--圓角 
	同時設定五個屬性radius無效
	radius設定四個角的半徑
	topLeftRadius左上角半徑
	bottomRightRadius右下角半徑-->
    <corners 
    android:radius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"/>
    
  	<!--設定控件的高和寬
         如果你在外面也設定了可能就覆寫了-->
 	<size 
    android:width="100dip"
    android:height="40dip"/>
    
    <!--間隔,各方向的間隔
        相當于padding設定paddingLeft、右、上、下一樣
        如果你在外面也設定了可能就覆寫了-->
	<padding 
    android:right="4dip"
    android:bottom="4dip"
    android:top="4dip"
    android:left="4dip"/>

	<!--填充的顔色 
	color 設定控件裡面填充的顔色-->
    <solid 
    android:color="@color/red_common"/>

	<!--
	描邊,設定控件的邊框
	width設定邊邊的寬度
	color設定邊邊的顔色
	dashGap 設定虛線的間隔寬度
	dashWidth 設定虛線的寬度
	隻要dashGap或dashWidth有一個0,就是實線-->
    <stroke 
	android:width="1dip"
    android:dashWidth="0dip"
    android:dashGap="10dip"
	android:color="@color/white"/>
	
	<!--漸變 
	注意:當設定填充顔色後,無漸變效果
	
	漸變角度angle 當angle=0時,漸變色是從左向右,
	然後逆時針方向轉,當angle=90時為從下往上,
	angle必須為45的整數倍,僅在type="linear"有效
	
	useLevel true和false 如果要使用
	LevelListDrawable對象,就要設定為true
	
	漸變類型 type  linear和radial和sweep
	linear 線性漸變,這是預設設定
    radial 放射性漸變,以開始色為中心
    sweep 掃描線式的漸變
    
    centerX 漸變中心X點坐标的相對位置
    centerY 漸變中心Y點坐标的相對位置
    
    gradientRadius 整型 漸變色半徑.
        當 android:type="radial" 時才使用,
        單獨使用 android:type="radial"會報錯。-->
    <gradient 
    android:startColor="@color/green_common"
    android:centerColor="@color/red_common"
    android:endColor="@color/blue"
    android:useLevel="true"
    android:angle="90"
    android:type="linear"
    android:centerX="0"
    android:centerY="0"
    android:gradientRadius="90"/>
     
</shape>
           

2.詳細講解

shape用于設定形狀,可以在selector,layout等裡面使用,有6個子标簽,各屬性如下:

android:shape=["rectangle" | "oval" | "line" | "ring"] shape的形狀,預設為矩形,可以設定為矩形(rectangle)、橢圓形(oval)、線性形狀(line)、環形(ring)

下面的屬性隻有在android:shape="ring時可用:

android:innerRadius 尺寸,内環的半徑。

android:innerRadiusRatio 浮點型,以環的寬度比率來表示内環的半徑,例如,如果android:innerRadiusRatio,表示内環半徑等于環的寬度除以5,這個值是可以被覆寫的,預設為9.

android:thickness 尺寸,環的厚度

android:thicknessRatio 浮點型,以環的寬度比率來表示環的厚度,例如,如果android:thicknessRatio="2",那麼環的厚度就等于環的寬度除以2。這個值是可以被android:thickness覆寫的,預設值是3.

android:useLevel boolean值,如果當做是LevelListDrawable使用時值為true,否則為false.

圓角

android:radius   整型 半徑

android:topLeftRadius   整型 左上角半徑

android:topRightRadius   整型 右上角半徑

android:bottomLeftRadius 整型 左下角半徑

android:bottomRightRadius 整型 右下角半徑

 漸變色

android:startColor  顔色值 起始顔色

android:endColor    顔色值 結束顔色

android:centerColor 整型   漸變中間顔色,即開始顔色與結束顔色之間的顔色

android:angle       整型   漸變角度(PS:當angle=0時,漸變色是從左向右。 然後逆時針方向轉,當angle=90時為從下往上。angle必須為45的整數倍)

android:type        ["linear" | "radial" | "sweep"] 漸變類型(取值:linear、radial、sweep) linear 線性漸變,這是預設設定;radial 放射性漸變,以開始色為中心;sweep 掃描線式的漸變

android:useLevel   ["true" | "false"] 如果要使用LevelListDrawable對象,就要設定為true。設定為true無漸變。false有漸變色

android:gradientRadius 整型 漸變色半徑.當 android:type="radial" 時才使用。單獨使用 android:type="radial"會報錯。

android:centerX     整型   漸變中心X點坐标的相對位置

android:centerY   整型   漸變中心Y點坐标的相對位置

内邊距,即内容與邊的距離

android:left   整型 左内邊距

android:top   整型 上内邊距

android:right   整型 右内邊距

android:bottom 整型 下内邊距

size 大小

android:width 整型 寬度

android:height 整型 高度

内部填充

android:color 顔色值 填充顔色

描邊

android:width 整型 描邊的寬度

android:color 顔色值 描邊的顔色

android:dashWidth 整型 表示描邊的樣式是虛線的寬度, 值為0時,表示為實線。值大于0則為虛線。

android:dashGap   整型 表示描邊為虛線時,虛線之間的間隔 即“

參考連結:

http://www.th7.cn/Program/Android/201503/401232.shtml

http://ju.outofmemory.cn/entry/126859

http://www.bkjia.com/Androidjc/1012885.html

http://blog.csdn.net/zhangxiweicaochen/article/details/41481463

http://blog.csdn.net/rflyee/article/details/20785495

http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/