在android自帶的控件中如按鈕,輸入框都不如ios的控件好看,我們一般都會仿照ios的控件的效果。只需在drawable目錄下新建一個xml文件,修改控件的樣式。
如button_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--點擊背景-->
<item android:state_pressed="true">
<shape>
<!--色值-->
<solid android:color="#e79429" />
<!--圓角-->
<corners android:radius="10dp" />
</shape>
</item>
<!--默認背景-->
<item>
<shape>
<solid android:color="#f6aa3e" />
<corners android:radius="10dp" />
</shape>
</item>
</selector>
把它設爲按鈕的背景,其效果:
一、android中的shape的標籤中的屬性有corners、gradient、padding、size、solid、stroke等。
1、Corners是用來字義圓角的,其中radius與其它四個並不能共同使用。。
<corners //定義圓角
android:radius="dimension" //全部的圓角半徑
android:topLeftRadius="dimension" //左上角的圓角半徑
android:topRightRadius="dimension" //右上角的圓角半徑
android:bottomLeftRadius="dimension" //左下角的圓角半徑
android:bottomRightRadius="dimension" /> //右下角的圓角半徑
2、solid(實心的) 用以指定內部填充色。
<solid android:color="color" />
3、gradient(梯度)用以定義漸變色,可以定義兩色漸變和三色漸變,及漸變樣式。
<gradient
android:type=["linear" | "radial" | "sweep"] //共有3中漸變類型,線性漸變(默認)/放射漸變/掃描式漸變
android:angle="integer" //漸變角度,必須爲45的倍數,0爲從左到右,90爲從上到下
android:centerX="float" //漸變中心X的相當位置,範圍爲0~1
android:centerY="float" //漸變中心Y的相當位置,範圍爲0~1
android:startColor="color" //漸變開始點的顏色
android:centerColor="color" //漸變中間點的顏色,在開始與結束點之間
android:endColor="color" //漸變結束點的顏色
android:gradientRadius="float" //漸變的半徑,只有當漸變類型爲radial時才能使用
android:useLevel=["true" | "false"] /> //使用LevelListDrawable時就要設置爲true。設爲false時纔有漸變效果
4、stroke這是描邊屬性,可以定義描邊的寬度,顏色,虛實線等。
<stroke
android:width="dimension" //描邊的寬度
android:color="color" //描邊的顏色
// 以下兩個屬性設置虛線
android:dashWidth="dimension" //虛線的寬度,值爲0時是實線
android:dashGap="dimension" /> //虛線的間隔
5、size和padding 這兩個基本上不怎麼用,因爲他們所具有的功能,控件本身也能實現。
二、Shape的屬性(rectangle、oval、line、ring ;矩形,還有橢圓形,線形和環形),Shape定義自已當前Shape的形狀。