我在下面的 xml 文件中定义了一个形状,现在我想以编程方式更改为纯色。
<?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>
我认为我应该有一个扩展 ShapeDrawable 并实现 onDraw 方法的类。有人知道怎么做吗?
最佳答案
终于搞定了!
// prepare
int strokeWidth = 5; // 5px not dp
int roundRadius = 15; // 15px 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);
关于android - 如何在 Java 代码中创建具有实体、角、笔划的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667964/