我想要使一个 ImageButton
处于 禁用状态(不可点击),但使用了 android:enabled="false"
并没有生效。
有人知道如何禁用一个 ImageButton
吗?
我想要使一个 ImageButton
处于 禁用状态(不可点击),但使用了 android:enabled="false"
并没有生效。
有人知道如何禁用一个 ImageButton
吗?
setClickable(false)
和setEnabled(false)
就可以达到效果。android:clickable
属性,或者在你的代码中使用setClickable(boolean)
方法。chevron_left.imageAlpha = 75 // 0 being transparent and 255 being opaque
chevron_left.isEnabled = false
启用:
chevron_left.imageAlpha = 255
chevron_left.isEnabled = true
XML:
<ImageButton
android:id="@+id/chevron_left"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="4dp"
android:layout_marginStart="4dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:src="@drawable/chevron_left"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
注意,您的背景颜色将定义禁用状态的颜色。取决于您想要的结果。
当为ImageButton
设置点击监听器时,在Android系统中,可点击属性会被重置为true
。这就是为什么在XML中设置android:clickable="false"
无效的原因。
此外,在XML中设置android:enabled="false"
也对我无效。
有效的方法是只通过代码设置:
ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);
ImageButton
和 ImageView
不同,没有 android:enabled="false"
属性,因为这是 TextView
的属性。如果你想在 XML 中为 ImageButton
设置 enable = false
,你需要添加 android:focusable="false"
和 android:clickable="false"
。