带参数的Android drawable

标签 android

有没有可能有类似的东西

drawable/myshape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">

    <solid android:color="{0}" />
</shape>

然后从java类中获取这个可绘制的参数传递值。有点像

Drawable myshape = ???.getDrawableWithParameters(R.drawable.myshape, 0x00ff00);

最佳答案

如果您按照示例中所示的方式创建一个形状,那么它可以作为GradientDrawable 访问。 .

GradientDrawable d = (GradientDrawable) getResources().getDrawable(R.drawable.yourid);

如果您查看文档(上面的链接),您可以看到所有功能,您可以从 GradientDrawable 对象访问。您也可以稍后使用此对象以编程方式设置背景和边框颜色和宽度。

我不是 100% 确定,每次您在 xml 中创建一个形状时,您都会得到一个 GradientDrawable。我需要自己修改形状的一些属性,并在尝试转换为其他类型的 Drawable

时发现看到 ClassCastExceptions

关于带参数的Android drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12396803/

相关文章:

全屏 Android Studio

android - 如何使工具栏透明并向后放置图像?

android - 当我们点击编辑文本时,我如何断言是否调用了软键盘

android - 需要在android中使用HttpPost上传文件

android - 使用 gradlew assembleRelease 从 native react 创建发布 apk 时出错

android - 如何将一个音频文件叠加在另一个文件上并保存?

java - Android 中的自动登录

安卓 : authentication to php server application

android - 未能加载属性文件...不再 :(

android - 将 native 代码添加到现有的 Worklight 混合应用程序