【问题标题】:Why does it go to the last onClick?为什么会到最后一个onClick?
【发布时间】:2013-11-20 18:36:10
【问题描述】:

我在可扩展列表视图中有一个带有这个 list_child 的 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp" >

    <ImageView
        android:id="@+id/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_clock"
        android:paddingLeft="30dp"
        android:contentDescription="@string/app_name"
        android:onClick="goMap"/>

    <ImageView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_map"
        android:paddingLeft="105dp"
        android:contentDescription="@string/app_name"
        android:onClick="goSchedule"/>

</RelativeLayout>

并且在两个ImageView中进入最后一个OnClick。为什么?

谢谢

【问题讨论】:

  • 你能解释一下你的问题吗?
  • 您的意思是两个 ImageView 触发相同的 onClick 方法,即 goSchedule()?请发布您的代码...
  • public void goSchedule(View view){ Intent i = new Intent(this, TimeTableActivity.class ); this.startActivity(i); }
  • 为什么不务实地使用 onClicklistener。

标签: android xml uiimageview onclick expandablelistview


【解决方案1】:

这是因为您没有正确对齐 imageView。 这样做:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp" >

    <ImageView
        android:id="@+id/clock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_marginRight="20dp"

        android:contentDescription="@string/app_name"
        android:onClick="goMap"/>

    <ImageView
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
       android:layout_toRightOf="@+id/clock"
        android:contentDescription="@string/app_name"
        android:onClick="goSchedule"/>

</RelativeLayout>

【讨论】:

  • 好的,非常感谢。你说的对。如果我想在两个 imageView 之间放置更多空间?
  • 将 marginLeft 或 marginRight 添加到您的 imageViews。试试我编辑的答案。
【解决方案2】:

您正在对 RelativeLayout 中的两个 ImageView 使用 paddingLeft。所以第二个 ImageView 正在克服第一个。尽管您同时看到了两个视图,但第二个视图位于第一个视图之上。使用 layout_marginLeft 属性而不是 paddingLeft。

<ImageView
    android:id="@+id/clock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:onClick="goMap"
    android:paddingLeft="30dp"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:onClick="goSchedule"
    android:layout_marginLeft="105dp"
    android:src="@drawable/ic_launcher" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多