【问题标题】:How to use Images as buttons in android? [duplicate]如何在android中使用图像作为按钮? [复制]
【发布时间】:2015-04-25 20:28:40
【问题描述】:

我正在开发一个食谱应用程序,它是一个沙拉食谱应用程序,我想在滚动视图中逐个添加每个沙拉项目的图像,当用户单击沙拉项目的图像时,我想显示其食谱详细信息.如何在我的应用程序中实现这一点,我可以使用图片作为按钮吗?有人可以帮忙吗??

我刚刚尝试将图像设置为按钮。是正确的方式吗??

我在 drawable 文件夹中创建了一个名为 images_button 的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/first" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/first_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/first_pressed" />
<item android:drawable="@drawable/first" />
</selector>

我的主要 xml 文件是

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_image"
    />

【问题讨论】:

  • 在您的 xml 文件中将 Button 更改为 ImageButton。你从哪里得到食谱的详细信息?
  • 我认为这是一个食谱列表,如果您单击其中一个用户可以查看食谱详细信息。为什么你不使用listView?在那里您可以添加每个项目imageView和简短描述,然后使用setOnItemClickListener使每个项目可点击

标签: android android-layout android-xml


【解决方案1】:

实际上您可以在每个 Android 视图上设置一个 OnClickListener,但我认为您应该查看具有自定义项目布局的 ListView 并使用 OnItemClickListener。

【讨论】:

    【解决方案2】:

    在你的布局xml文件中直接使用ImageButton

    <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/button_image" />
    

    【讨论】:

      【解决方案3】:

      您可以使用ImageButton。它的行为就像一个普通的按钮,只是它能够显示图像

      <ImageButton
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/some_image" />
      

      【讨论】:

        【解决方案4】:

        是的,您可以使用 ImageView 将图片用作按钮并设置 onclick 侦听器。

        或者,您还可以创建一个列表视图或网格视图,其中列表项是图像并使用 onitemclick 侦听器。

        还有其他建议的图片按钮。

        我认为 ImageView 将满足您的要求。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-07
          • 2019-10-24
          • 2011-08-20
          • 2020-06-20
          • 1970-01-01
          • 2011-12-10
          相关资源
          最近更新 更多