Android xml設置各種背景

記錄一下xml設置各種背景,不然經常翻之前的代碼,哈哈

1.設置圓角背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fcfcfc" /> 背景顏色
    <corners android:radius="5dp"/> 圓角大小
    //邊框顏色和寬度,可選
    <!--<stroke android:width="0.5dp" android:color="@color/colorPrimary" />--> 
</shape>

2.設置圓形背景

<?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="@color/white"/> 背景顏色
    <stroke android:width="1.5dp" android:color="#ff2003" /> 邊框可選
    <size  //大小不必要
        android:width="15dp"
        android:height="15dp" />
</shape>

3.背景上/下/左/右帶邊框

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- This is the main color -->
    <item>
        <shape>
            <solid android:color="@color/color_line"/>//邊框顏色
        </shape>
    </item>
    //根據需要自己設置顯示邊框的未知 top left right bottom
    <item android:bottom="0.8dp"  android:top="0.8dp"> 
        <shape>
            <solid android:color="@color/white" />//填充顏色
        </shape>
    </item>
</layer-list>

4.邊框設置虛線

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#f5f6f7" /> //填充顏色
    <corners android:radius="4dp"/> //圓角大小
    <stroke android:width="1dp" android:color="#ccc" //邊框寬度和顏色
        android:dashGap="3dp" //間隔距離
        android:dashWidth="5dp"/>//虛線的長度
</shape>

5.設置漸變色-圓角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:endColor="#35a3ff" //漸變結束顏色
        android:startColor="#6bd7ff" />//漸變開始顏色
    <corners android:radius="50dp" /> // 圓角大小
</shape>

6.設置漸變色-圓形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    //開始結束顏色
    <gradient android:startColor="#6bd7ff" android:endColor="#35a3ff" /> 
    //邊框可選
	<!--    <stroke android:width="1dip" android:color="#000000" />-->
    <size
        android:width="15dp"
        android:height="15dp" />
</shape>

7.背景上/下/左/右帶圓角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    //填充顏色
    <solid android:color="@color/white" />
    //設置圓角位置
    <corners android:topRightRadius="5dp" android:bottomRightRadius="5dp"/>
    //邊框顏色可寬度
    <!--<stroke android:width="0.5dp" android:color="@color/lineBGColor"/>-->
</shape>

8.設置文字選中顏色

//TextView 設置textColor屬性
android:textColor="@drawable/color_text"
//color_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#40464f" android:state_checked="true"></item>
    <item android:color="@color/white" android:state_checked="false"></item>
</selector>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章