【问题标题】:how to create clickable elements inside an imageView?如何在 imageView 中创建可点击元素?
【发布时间】:2018-05-12 11:07:41
【问题描述】:

我正在尝试在我的 android 应用程序中的 imageView 内点击可点击元素,但没有找到任何方法。

我的目标是做和这个网站一样的事情:https://listed.life/hubbox-flyer/

希望您能理解我的要求,感谢您抽出宝贵时间回答我的问题。

【问题讨论】:

  • I hope you understand my request 是的 但是到目前为止您尝试了什么?你身边的任何努力

标签: android imageview


【解决方案1】:

如果我理解正确,您只是想让 ImageView 可点击?如果是这种情况,您应该设置一个 OnClickListener。

在你的 .xml 文件中你给你的 imageView 一个 id,比如说android:id="@+id/iv_product"

然后,在您的 Activity 中,您可以使用 findViewById 找到您的 imageView,如下所示: ImageView imageView= findViewById(R.id.iv_product)

最后,设置 OnClickListener:

imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ // Do whatever you want here } });

【讨论】:

  • 感谢您的回答,但我希望它可以让 imageView 的某些部分像在网站上一样可点击 listed.life/hubbox-flyer
  • 恐怕你说的没有道理。您提供的网页中的所有“imageViews”都是可点击的。无论如何,也许你需要实现一个布局,它将由一个可点击的 ImageView 和一个 TextView 或另一个不可点击的 ImageView 组成......
【解决方案2】:

使用此代码`

 <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
           <Button
               android:id="@+id/button"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:background="?attr/selectableItemBackground"/>
           <ImageView
               android:id="@+id/image"
               android:paddingTop="?attr/actionBarSize"
               android:layout_width="match_parent"
               android:layout_gravity ="center"
               android:scaleType="fitXY"
               android:layout_marginTop="15dp"
               android:layout_marginBottom="5dp"
               android:background="#e6e4e4"
               android:visibility="visible"
               android:layout_height="200dp" />

       </RelativeLayout>

然后在按钮上添加 setOnClickListener。 有关更多信息,请查看 android 介绍滑块代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 2011-04-06
    • 2019-08-04
    • 2019-11-07
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多