【问题标题】:Difference between Button with image, ImageButton, and clickable ImageView?Button with image, ImageButton, and clickable ImageView的区别?
【发布时间】:2012-03-14 07:03:26
【问题描述】:

Button with image, ImageButton 和 clickable ImageView 有什么区别吗?

【问题讨论】:

    标签: android button imagebutton


    【解决方案1】:

    这可能只涵盖了部分差异,实际查看 Android 源代码树以了解发生了什么会有所帮助。

    ImageButtons 具有推送状态,而可点击图像则没有。 你也不能为 ImageButton 调用 setText,你可以用一个普通的按钮。

    它们都来自视图,但查看以下扩展链可能会有所帮助。

    java.lang.Object
    ↳ android.view.View
       ↳ android.widget.ImageView
           ↳ android.widget.ImageButton
    

    java.lang.Object
    ↳ android.view.View
       ↳ android.widget.TextView
           ↳ android.widget.Button
    

    【讨论】:

    • 有趣。另一个由缺乏(某种)多重继承引起的问题的例子。
    • 何时使用一种与另一种?
    • 什么是推送状态?
    • @ShikharMainalee 按下/单击按钮时的状态。也称为压缩或关闭状态。
    【解决方案2】:

    差异可能很微妙。理解这一点的最直接方法是从查看文档开始。如果您查看docs for Button,您会发现Button 派生自TextView。另一方面,ImageButton 派生自 ImageView。所以从根本上说,Button 可以有文本并且是可点击的,而ImageButton 在设置图像方面更灵活一些。它具有来自其ImageView 基类的方法,例如setImageURI,而Button 没有。这两者与普通的ImageView 之间的区别之一是您可以拥有按钮状态,这在ButtonImageButton 文档中都有说明。

    【讨论】:

      【解决方案3】:
      ImageView = Display Images (android:src)
      

      ImageButton = Diaplay 图像为imageViewget click 效果为按钮(android:src),并且无法为其设置文本。

      Button = set text and (android:background)
      

      【讨论】:

      • 谢谢,这是设置的语法差异。我需要知道实际差异(何时使用或不同的外观)
      【解决方案4】:

      前面的答案中没有提到的另一个方面是(例如)列表项视图中的用法。如果您嵌入了 Button 或 ImageButton,则列表项的其余部分将不会接收到触摸事件。但是如果你使用 ImageView,它会的。

      【讨论】:

      • 你有这方面的资料吗?我刚刚用ImageButton 遇到了这个问题,但Button 没有图像背景
      • 恐怕我无法再访问相关源。但这很简单——按钮表单吃掉了触摸事件,而图像视图没有。我确信还有其他方法可以将事件传递到上游,但这是一个不需要重构的简单修复。
      【解决方案5】:
      button instanceof ImageButton == false;
      imageButton instanceof Button == false;
      button instanceof TextView == true;
      imageButton instanceof ImageView == true;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-16
        • 2013-08-13
        • 1970-01-01
        • 2012-12-12
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        相关资源
        最近更新 更多