gpt4 book ai didi

android - 如何将ImageButton的前景图片设置为空

转载 作者:行者123 更新时间:2023-11-29 18:04:23 25 4
gpt4 key购买 nike

我有一些 ImageButtons 被用作分段控件,每个都有一个背景集,前景图像将是一个复选标记,显示当前选择了 3 个中的哪一个。其他 2 个按钮应该没有前景图像。图像以 XML 定义(见下文)。

            <ImageButton
android:id="@+id/style_light_button"
android:layout_width="@dimen/style_color_segment_width"
android:layout_height="@dimen/style_button_segment_height"
android:background="@drawable/button_segmented_light"
android:src="@drawable/icons_checkmark_dark" />

<ImageButton
android:id="@+id/style_sepia_button"
android:layout_width="@dimen/style_color_segment_width"
android:layout_height="@dimen/style_button_segment_height"
android:background="@drawable/button_segmented_sepia"
android:src="@drawable/icons_checkmark_dark" />

<ImageButton
android:id="@+id/style_dark_button"
android:layout_width="@dimen/style_color_segment_width"
android:layout_height="@dimen/style_button_segment_height"
android:background="@drawable/button_segmented_dark"
android:src="@drawable/icons_checkmark_light" />

在代码中,当点击一个时,我将清除未点击的 2 个复选标记,并确保将其添加到被点击的那个。

ImageButton lightModeButton = (ImageButton)findViewById(R.id.style_light_button);
ImageButton sepiaModeButton = (ImageButton)findViewById(R.id.style_sepia_button);
ImageButton darkModeButton = (ImageButton)findViewById(R.id.style_dark_button);

我已经尝试了 setImageBitmap(null)setImageDrawable(null) 但它们都崩溃了。

lightModeButton.setImageBitmap(null);
sepiaModeButton.setImageDrawable(null);
darkModeButton.setImageResource(R.drawable.icons_checkmark_light);

我怎样才能清除图像,或者只隐藏前景图像,同时保留背景图像显示?

最佳答案

来自 android documentation :

 public void setBackgroundResource (int resid)

将背景设置为给定资源。资源应引用 Drawable 对象或 0 以移除背景。

我不知道它是否适用于 setImageResource(int resId) 所以试一试:

sepiaModeButton.setImageResource(0);

代替

sepiaModeButton.setImageDrawable(null);

关于android - 如何将ImageButton的前景图片设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14140472/

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