Android 代码创建Shape属性(solid, corne
2016-01-04 本文已影响8937人
simpleeeeee
UI设计图都是带圆角的,简单写一个 Shape 属性搞定。但是需要每个 Shape 属性的背景颜色都不一样,那就需要在代码中直接创建 Shape 属性。
我个人是很不喜欢圆角的设计,现在应用图标也改成了圆角,点击应用图标我都有负担啊。
<?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代码设置Shape,corners,Gradient