Android 常用布局文件:Android 常用 shape 整理

说明:大部分内容都是参考别的文章,这里做整理是为了以后的编程有实用的模板,可以即需即用。

1、圆

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

    <solid android:color="#c6c6c6" />
    <size
        android:width="50dp"
        android:height="50dp" />

</shape>

2、带圆角的长方形

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#b2b2b2" />
    <size
        android:width="50dp"
        android:height="30dp" />
    <corners android:radius="5dp" />
</shape>

3、带圆角的边框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="#ff5a85" />
    <size
        android:width="50dp"
        android:height="30dp" />
    <corners android:radius="5dp" />
</shape>

4、状态切换

// selector_switch_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_open" android:state_checked="true" />
    <item android:drawable="@drawable/btn_close" android:state_checked="false" />
</selector>
<ToggleButton
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:background="@drawable/selector_switch_button"
        android:textOff="@null"
        android:textOn="@null" />
        
<Switch
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:background="@drawable/selector_switch_button"
        android:textOff="@null"
        android:textOn="@null" />

5、水平进度条背景

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

    <!-- 设置progressbar的背景样式 -->
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="20dp" />
            <solid android:color="#ffffff" />
            <stroke
                android:width="1dp"
                android:color="#32dcdc" />
        </shape>
    </item>

    <!-- 设置progressbar的进度条样式 -->
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <shape>
                <corners android:radius="20dp" />
                <solid android:color="#32dcdc" />
            </shape>
        </scale>
    </item>

</layer-list>
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:max="100"
        android:progressDrawable="@drawable/progress_bg" />

6、渐变色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:endColor="#ff6060"
        android:startColor="#ff2434" />
</shape>
<gradient
        android:angle="integer"  // Integer,代表渐变颜色的角度,必须是45的整数倍,默认是 0,需要 android:type="linear"
        android:centerX="integer"  // Float,相对X的渐变位置(只有在 type 不为 linear 的情况下起作用)
        android:centerY="integer"  // Float,相对Y的渐变位置(只有在 type 不为 linear 的情况下起作用)
        android:centerColor="integer"  // Color,颜色渐变的中间颜色,主要用于多彩
        android:endColor="color"  // Color,颜色渐变的结束颜色
        android:startColor="color"  // Color颜色渐变的开始颜色
        android:gradientRadius="integer"  // Float,渐变颜色的半径,单位应该是像素点. 需要 android:type="radial"
        android:type=["linear" | "radial" | "sweep"]
        // linear 线性渐变,默认值
        // radial 放射性渐变(圆形渐变),起始颜色从 cenralX,centralY点开始
        // sweep 扫描式渐变(扇形渐变)
        android:useLevel=["true" | "false"] />  // 使用 LevelListDrawable 时就要设置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章