Android 自定義View直線、圓形、橢圓、實心圓、矩形等

 項目中經常用到各種各樣的樣式,在此記錄下;

直線:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="5dp"
        android:color="@color/blue" />
</shape>

圓形:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="@color/blue" />
    <!--定義寬和高相等的話就是圓形-->
    <size
        android:width="100dp"
        android:height="100dp" />
</shape>

橢圓:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="3dp"
        android:color="@color/blue" />
</shape>

實心圓:用 Stroke描邊,對應 Paint.STROKE ,而用Solid 填充,對應 Paint.FILL,會出現實心圓形。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <!-- 漸變色-->
    <gradient
        android:endColor="@color/blue"
        android:startColor="@color/red" />
    <size
        android:width="100dp"
        android:height="100dp" />

</shape>

矩形:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角-->
    <corners android:radius="5dp" />
    <!-- 高度-->
    <stroke
        android:width="1dp"
        android:color="@color/blue" />
    <!-- 大小-->
    <size
        android:width="100dp"
        android:height="100dp" />

</shape>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章