Android Drawable之Shape使用小案例(一)

日常开发中少不了自己画shape,那么接下来就通过几个小案例来学习一下吧
效果图

按照上图先后顺序的效果
圆角矩形


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    

    
    <solid android:color="#B848FF">solid>

    
    <stroke
        android:width="2dp"
        android:color="#777777">stroke>

    
    <corners android:radius="5dp" />

shape>

圆角矩形虚线


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    
    <solid android:color="#FFFFFF">solid>

    
    <stroke
        android:width="1dp"
        android:color="#D5D5D5"
        android:dashGap="3dp"
        android:dashWidth="10dp">stroke>

    
    <corners android:radius="10dp" />
shape>

注:破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线

圆角矩形背景渐变


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    

    
    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    
    <gradient
        android:angle="0"
        android:endColor="#FFFFFF"
        android:startColor="#D5D5D5" />

    

    <stroke
        android:width="1dp"
        android:color="#D5D5D5" />
shape>

圆形


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    

    
    <solid android:color="#B848FF">solid>

    
    <stroke
        android:width="2dp"
        android:color="#777777">stroke>

    
    <corners android:radius="5dp" />

shape>

注:这里需要主要的是,在使用圆形shape时,必须将控件宽度和高度设置为一样

布局文件


<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"
    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="com.example.drawableshapoe.MainActivity">

    <TextView
        android:layout_width="200dp"
        android:layout_height="25dp"
        android:background="@drawable/rounded_rectangle_shape"
        android:gravity="center_vertical|center_horizontal"
        android:text="Hello World!" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="25dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded_rectangle_dotted_line_shape"
        android:gravity="center_vertical|center_horizontal"
        android:text="Hello World!" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="25dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded_half_rectangle_gradient_shape"
        android:gravity="center_vertical|center_horizontal"
        android:text="Hello World!" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded_oval_shape"
        android:gravity="center_vertical|center_horizontal"
        android:text="Hello World!" />
LinearLayout>

源码
github:https://github.com/Waylenw/AndroidBase

你可能感兴趣的:(Android)