【发布时间】:2013-01-03 22:27:59
【问题描述】:
我有一些 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);
如何清除图像,或者只隐藏前景图像而保留显示背景图像?
【问题讨论】:
-
只需设置imgButton.setImageResource(0);
-
好的,试过了,但仍然出现空指针崩溃,所以我想我的问题是按钮由于某种原因为空。谢谢。
-
@jamone - 可能。 setImageDrawable(null);应该与 setImageResource(0); 一起正常工作