UI設(shè)計圖都是帶圓角的盒延,簡單寫一個 Shape 屬性搞定。但是需要每個 Shape 屬性的背景顏色都不一樣痹换,那就需要在代碼中直接創(chuàng)建 Shape 屬性蝗羊。
我個人是很不喜歡圓角的設(shè)計,現(xiàn)在應(yīng)用圖標也改成了圓角统翩,點擊應(yīng)用圖標我都有負擔啊仙蚜。
<?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