UI設(shè)計(jì)圖都是帶圓角的,簡單寫一個(gè) Shape 屬性搞定。但是需要每個(gè) Shape 屬性的背景顏色都不一樣,那就需要在代碼中直接創(chuàng)建 Shape 屬性。
我個(gè)人是很不喜歡圓角的設(shè)計(jì),現(xiàn)在應(yīng)用圖標(biāo)也改成了圓角,點(diǎn)擊應(yīng)用圖標(biāo)我都有負(fù)擔(dān)啊。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#DFDFE0" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
<stroke
android:width="3dp"
android:color="#2E3135" />
</shape>
// prepare
int strokeWidth = 5; // 3px not dp
int roundRadius = 15; // 8px not dp
int strokeColor = Color.parseColor("#2E3135");
int fillColor = Color.parseColor("#DFDFE0");
GradientDrawable gd = new GradientDrawable();
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
參考資料
How to create shape with solid, corner, stroke in Java Code
Android代碼設(shè)置Shape,corners,Gradient