我只是想知道ImageView
设置为可点击与ImageButton
相比是否有任何显着差异?可点击的ImageView和ImageButton之间的区别
是否有任何理由使用其中一种? ImageButton
是否有任何限制作为唯一可能的选项的可绘制?
如果我选择可点击的ImageView
而不是ImageButton
,我可能会失去按钮的任何功能吗?
我只是想知道ImageView
设置为可点击与ImageButton
相比是否有任何显着差异?可点击的ImageView和ImageButton之间的区别
是否有任何理由使用其中一种? ImageButton
是否有任何限制作为唯一可能的选项的可绘制?
如果我选择可点击的ImageView
而不是ImageButton
,我可能会失去按钮的任何功能吗?
除了默认样式,没有区别。 ImageButton
默认情况下具有非空背景。
编辑:另外,ImageButton.onSetAlpha()
方法始终返回false,scaleType
设置为center
而且总是夸大为可获得焦点。
这里的ImageButton
的默认样式:
<style name="Widget.ImageButton">
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="android:background">@android:drawable/btn_default</item>
</style>
的ImageButton从ImageView的
public class ImageButton extends ImageView {
public ImageButton(Context context) {
this(context, null);
}
public ImageButton(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.imageButtonStyle);
}
public ImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
}
@Override
protected boolean onSetAlpha(int alpha) {
return false;
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(ImageButton.class.getName());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(ImageButton.class.getName());
}
继承的@Micheal形容我只是添加细节,他的回答
按钮的作用点击时,我点击是为了图像按钮,但不是为imageView。
感谢您的回答。当我自己浏览代码时,你给了我更多的东西。我想一天结束,2之间的选择将取决于您可以使用多少默认属性,而无需任何定制。 – yjw 2011-05-01 15:24:17
不客气!是的,确实没有太大的差别,所以我认为选择是在按钮和不按钮之间。 – Michael 2011-05-01 16:29:23
实际上,根据我的经验,两者之间的另一个区别是,如果您想将可点击的按钮放入ListView的单元格中,同时将单元格本身保持为可点击,则使用Imageview会更好。当TextViews和ImageViews不用时,EditTexts和ImageButton似乎会消耗触摸事件。 – 2012-11-29 11:36:04