【发布时间】:2025-12-01 08:45:01
【问题描述】:
如何以编程方式创建此形状?
<?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()方法
【问题讨论】:
-
为什么不使用多种样式?
-
@Droidekas 如果我无法创建自定义函数来创建它,我必须拥有超过 20 个样式的 xml 文件
-
您尝试过使用自定义视图吗?
-
@Droidekas 不,先生,您能介绍一些教程或帮助我吗?顺便说一下更新后