gpt4 book ai didi

android-layout - 访问 XML 可绘制资源中元素的属性

转载 作者:行者123 更新时间:2023-11-30 02:48:17 25 4
gpt4 key购买 nike

请原谅标题 - 不知道如何表达这个问题。

我在 res/drawable 中创建了一个名为 rectangle.xml 的文件。它是一个非常基本的矩形形状,我在自定义 ListView 中与 ImageView 一起使用。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FF0000" />
<corners android:radius="4dp"/>
</shape>

像这样使用它:

<ImageView
android:id="@+id/row_colorRect"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/rectangle" />

我希望能够访问该矩形的 solid 属性,以便即时更改其颜色。我无法通过 ImageView 访问该属性。所以我想我必须以某种方式深入了解 ImageView 的子项,找到形状,然后以这种方式进行更改。

我该怎么做?

谢谢!

最佳答案

刚试过,我成功了。

ImageView imageView = (ImageView) findViewById(R.id.row_colorRect);
((GradientDrawable) imageView.getDrawable()).setColor(Color.RED);

访问代表您的 rectangle.xml 资源的 ImageView GradientDrawable,您可以更改其纯色和其他属性。

关于android-layout - 访问 XML 可绘制资源中元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24642205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com