【问题标题】:Differentiate between ImageButton and ImageView? [duplicate]ImageButton和ImageView的区别? [复制]
【发布时间】:2017-10-23 06:52:31
【问题描述】:

ImageView 和 ImageButton 之间有点混淆。我认为两者都具有基本相同的 XML 属性。如果我使用 ImageView 而不是 ImageButton,对应用程序有什么影响吗?

【问题讨论】:

  • ImageButton 默认具有非空背景。除此之外,没有任何区别。
  • 如果没有区别,为什么要分开?#peter haddad
  • 对于 ImageView,您必须为 clickevent 添加 onClick 属性。但是 ImageButton 你不需要它,因为它提供了按钮的属性。
  • 已经在这里回答:stackoverflow.com/questions/5847136/…
  • 正如我所说,ImageButton 具有非空背景,而 ImageView 没有。如果您使用 ImageButton,您可能会在 ImageView 只有一张图片的背景中看到类似感觉的按钮。

标签: java android xml android-layout


【解决方案1】:

ImageButton 和 ImageView 的区别只有默认样式。默认情况下,ImageButton 具有非空背景。 ImageButton 的默认属性:

  • ImageButton.onSetAlpha() 方法总是返回 false。
  • 它的 scaleType 设置为 center,并且
  • 它总是膨胀为可聚焦的。

【讨论】:

    【解决方案2】:

    显示一个带有用户可以按下或单击的图像(而不是文本)的按钮。默认情况下,ImageButton 看起来像一个普通的 Button,标准的按钮背景会在不同的按钮状态下改变颜色。按钮表面上的图像由 XML 元素中的 android:src 属性或 setImageResource(int) 方法定义。

    ImageButton

    ImageButton 继承自 ImageView。默认情况下,图像按钮将具有 imageButtonStyle 作为样式。看一下ImageButton的源码

    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 defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }
    
    public ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setFocusable(true);
    }
    
    @Override
    protected boolean onSetAlpha(int alpha) {
        return false;
    }
    
    @Override
    public CharSequence getAccessibilityClassName() {
        return ImageButton.class.getName();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多