【问题标题】:Android background selector reacts to touch outside its viewAndroid 背景选择器会对其视图外的触摸做出反应
【发布时间】:2014-01-14 14:07:28
【问题描述】:

我有以下布局,可以在屏幕上均匀缩放 4 个视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <include
            android:id="@+id/one"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            layout="@layout/item" />

        <include
            android:id="@+id/two"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            layout="@layout/item" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <include
            android:id="@+id/three"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            layout="@layout/item" />

        <include
            android:id="@+id/four"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            layout="@layout/item" />
    </LinearLayout>

</LinearLayout>

包含中使用的 item.xml 如下所示:

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

    <RelativeLayout
        android:id="@+id/bg_layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/bg_selector" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            android:ellipsize="end"
            android:padding="4dp"
            android:singleLine="true"
            android:text="Headline"
            android:textAppearance="@style/SmallTextBold"
            android:textColor="#ffffff" />
    </RelativeLayout>

</RelativeLayout>

结果看起来像我所期望的那样:

问题是当我触摸其中一个项目/视图时,所有的背景都会发生变化,它们都会对触摸一个项目/视图做出反应:

这可能是什么问题?它与包含有关吗?

编辑:我刚刚将 &lt;include&gt;s 替换为包含的 item.xml 布局的实际内容,并为这些布局提供了唯一的 ID,但问题仍然存在。

EDIT2:我多次清理我的项目,但无济于事。我继续摆弄布局,但无济于事。通过 GIT 将所有内容重置为我开始使用的内容,现在它正在工作。很奇怪。

【问题讨论】:

    标签: android android-layout android-view


    【解决方案1】:

    您代码中的点击管理器可能指向您包含的每个项目的相同 ID。

    尽量不要使用包含或编码您自己的自定义视图。

    【讨论】:

    • 谢谢,但删除包含没有帮助:(
    • 你是否也改变了每个视图的ID?
    • 告诉我,我很好奇这种奇怪的效果。使用include的时候看到了很多奇怪的东西,有些已经通过清除和重新编译(如果使用eclisp)项目解决了!
    • 是的,我做到了。我还将每个保持背景的布局的 ID 更改为唯一。
    • 我没有办法解决,尝试在有背景的视图中放一个android:clickable="true",能否也给出背景xml的来源?
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 2013-05-14
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多