这个demon主要演示了drawable中shape的用法。
主要在布局文件中引用在drawable中定义的shape
shape1.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 用红色填充drawable并为其设置宽度为5dp,颜色为绿色的描边 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffff0000"/>
<stroke android:width="5dp" android:color="#ff00ff00"/>
</shape>
line.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 画一条线。这里需要注意的是stroke android:width="2dp" 属性定义的是线的高度
android:dashWidth="5dp" android:dashGap="4dp"表示每段宽5dp间隔4dp的虚线
size android:height="10dp"表示整个drawable的高度,而线在drawable的垂直方向上剧中
所以如果height小于或等于width则线是不可见的 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke android:width="2dp" android:color="#ff0000ff"
android:dashWidth="5dp" android:dashGap="4dp"/>
<size android:height="10dp"/>
</shape>
shape2.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 用红色填充drawable并为其设置宽度为5dp,颜色为绿色的虚线描边
设置圆角的半径为4dp
padding属性表明在drawable上面显示的内容(比如文字)距离drawable各个边框的距离-->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffff0000"/>
<stroke android:width="5dp" android:color="#ff00ff00"
android:dashWidth="5dp" android:dashGap="4dp"/>
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="4dp" />
</shape>
shape3.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 用红色填充drawable(椭圆形)并为其设置宽度为5dp,颜色为绿色的虚线描边
设置圆角的半径为4dp
padding属性表明在drawable上面显示的内容(比如文字)距离drawable各个边框的距离-->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#ffff0000"/>
<stroke android:width="5dp" android:color="#ff00ff00"
android:dashWidth="5dp" android:dashGap="4dp"/>
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="4dp" />
</shape>
shape4.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 用渐变颜色填充drawable(矩形)设置圆角的半径为8dp
padding属性表明在drawable上面显示的内容(比如文字)距离drawable各个边框的距离-->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="50dip"
android:src="@drawable/shap_1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/line"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dip"
android:src="@drawable/shap_2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/line"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dip"
android:src="@drawable/shap_3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/line"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="50dip"
android:src="@drawable/shap_4"/>
</LinearLayout>
</ScrollView>