gpt4 book ai didi

android - 如何替换 xml 项目的可绘制源

转载 作者:行者123 更新时间:2023-11-29 00:44:36 25 4
gpt4 key购买 nike

我有一个用于按钮的可绘制选择器 (home_button.xml),例如...

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/blue_button" />
<item android:state_pressed="true"
android:drawable="@drawable/red_button" />
</selector>

在我的主要 Activity 布局中,我将这个可绘制对象用作

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home_button" android:text="@string/home_button"
android:background="@drawable/home_button" ></Button>

现在我需要在基于用户登录的 Activity 中显示动态远程图像来代替选择器 xml 的本地 blue_button.png 和 red_button.png。

如何做到这一点?

最佳答案

在 Android 上 Button你得到的类文档:

If you're not satisfied with the default button style and want to customize it to match the design of your application, then you can replace the button's background image with a state list drawable.

您可能想用您喜欢的 StateListDrawable 替换图像按钮背景可绘制对象 SO发布,假设您已经下载了两个图像并将它们作为可绘制对象

    StateListDrawable states = new StateListDrawable();
//This for pressed true
states.addState(new int[] {android.R.attr.state_pressed},
drawable_image1);
//This for pressed false
states.addState(new int[] { },
drawable_image2);
//Change it on the button
button.setBackgroundDrawable(states);

关于android - 如何替换 xml 项目的可绘制源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136780/

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