读书人

ShapeDrawable的使用 textview设立

发布时间: 2013-10-08 17:12:09 作者: rapoo

ShapeDrawable的使用 textview设置

ShapeDrawable的使用

textview设置渐变色,背景填充色,边框,等统统搞定。

效果图:

ShapeDrawable的使用  textview设立

下面是依次定义的shape资源的.xml文件:

1:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >


<!-- 设置填充颜色 -->
<solid android:color="#ff0033" />
<!-- 设置四周的内边距 -->
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" />
<!-- 设置变框 -->
<stroke
android:width="1dip"
android:color="#ff0" />

</shape>

2:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义填充渐变颜色 -->
<gradient android:startColor="#ffff0000"
android:endColor="#80ff00ff"
android:angle="45"
android:type="linear"/>
<!-- 设置四周的内边距 -->
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" />
<!-- 设置圆角矩形 -->
<corners android:radius="10dp"/>


</shape>

3:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >


<!-- 定义填充渐变颜色 -->
<gradient android:startColor="#ff0"
android:endColor="#00f"
android:angle="45"
android:type="sweep"/>
<!-- 设置四周的内边距 -->
<padding
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" />
<!-- 设置圆角矩形 -->
<corners android:radius="10dp"/>
</shape>

4:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >


<!-- 设置填充颜色 -->
<solid android:color="#ff0033" />

<!-- 设置变框 -->
<stroke
android:width="1dip"
android:color="#ff0" />
<corners
android:topLeftRadius="10px"
android:topRightRadius="35px"
android:bottomLeftRadius="50px"
android:bottomRightRadius="80px"/>


</shape>

下面是布局layout中的使用:

<LinearLayout 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:orientation="vertical" >


<TextView
android:layout_margin="20dp"
android:background="@drawable/shape_style1"
android:id="@+id/shape_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_margin="20dp"
android:background="@drawable/shape_style2"
android:id="@+id/shape_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:layout_margin="20dp"
android:background="@drawable/shape_style3"
android:id="@+id/shape_tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:padding="20dp"
android:layout_margin="20dp"
android:background="@drawable/shape_style4"
android:id="@+id/shape_tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />


</LinearLayout>


源码下载


读书人网 >移动开发

热点推荐