實例展示
開發流程
Step-1 定義shape樣式
在項目的資源文件夾(res -> drawable)下,新建一個Drawable resource file,命名為shape_circular.xml,詳細代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<padding android:left="1dp"
android:top="2dp"
android:right="1dp"
android:bottom="2dp"/>
<solid android:color="#f17373"/>
<stroke android:color="#ffffff"
android:width="1dp"/>
<size android:width="15dp"
android:height="15dp"/>
</shape>Step-2 在布局文件中引用樣式
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ysu.edu.firstactivity.MainActivity">
<TextView
android:id="@+id/txtNum"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:text="0"
android:textColor="#ffffff"
android:textSize="50dp"
android:gravity="center"
android:elevation="20dp" /設置陰影/
android:background="@drawable/shape_circular"/> /引用樣式/
</RelativeLayout>-
總結
按照以上步驟即可實現圖示的效果。下面簡單總結一下Shape屬性的用法:
可在shape頭標簽中定義類型的形狀
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
shape有四種取值:rectangle(默認形狀)、oval、line、ring
分別代表:長方形、橢圓形、線形、環形- corners 圓角: 創建圓角的形狀,僅適用于當其形狀是一個長方形
- gradient 漸變: 指定一個漸變顏色的形狀
- padding 間隔: 適用于包含填充視圖元素
- size 尺寸: 定義形狀的大小
- solid 填充:定義形狀的填充顏色
- stroke 描邊:定義形狀的邊框顏色和寬度
查看更多詳細內容可點擊鏈接:http://blog.csdn.net/dblackde/article/details/7370467