我想留下一个ImageButton被禁用(不可点击),但已经使用android: enabled = "false"并且不起作用。
有谁知道如何禁用一个ImageButton?
我想留下一个ImageButton被禁用(不可点击),但已经使用android: enabled = "false"并且不起作用。
有谁知道如何禁用一个ImageButton?
如果您想将按钮显示为禁用(如果您在 xml 可绘制文件中设置了该按钮),则同时执行setClickable(false)AND即可setEnabled(false)。
您可以使用android:clickableXML 上的属性或setClickable(boolean)代码中的方法。
为 设置 clicklistener 时ImageButton,在后台 android 会将 clickable 属性重置为true. 这就是为什么android:clickable="false"在 xml 中设置没有帮助。
此外,android:enabled="false"在 xml 中设置属性对我也不起作用。
起作用的只是通过代码设置:
ImageButton mBtnDelayCall = (ImageButton)v.findViewById(R.id.btnCallDelay);
mBtnDelayCall.setEnabled(false);
如果您想禁用图像并将其“变灰”,我使用以下(Kotlin):
禁用:
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"/>
请注意,您的背景颜色将定义禁用状态的颜色。取决于你想要的结果。
ImageButtonlikeImageView没有android:enabled="false"属性,因为它是 的属性TextView。如果你想enable = false在 XML 中生成,ImageButton你必须添加android:focusable="false"和android:clickable="false".