如何以编程方式创建这个形状?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#e67e22"/>
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
</shape>
我尝试过这个简单的函数,它获取角、颜色并将其设置为形状:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.category_header);
GradientDrawable drawable = (GradientDrawable) linearLayout.getDrawable();
float[] values = { 0.2f, 0.2f, 0.2f, 0.2f };
drawable.setCornerRadii(values);
但我收到了这个错误:
LinearLayout 类型的 getDrawable() 方法未定义
你可以这样做:
public static void customView(View v, int backgroundColor, int borderColor) {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
shape.setColor(backgroundColor);
shape.setStroke(3, borderColor);
v.setBackground(shape);
}
See the 文档 https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setCornerRadii(float%5B%5D)的意义setCornerRadii
params.
您可以在整个应用程序中使用此功能,并可以设置您选择的边框和背景颜色。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)