为什么在充气布局上附加触摸侦听器而不注册触摸事件?

时间:2013-08-01 04:39:59

标签: android android-layout

因此,在开发我的Android应用程序时,我遇到了一个稍微有点时髦的行为,我很好奇它为什么会发生。当我执行以下操作时,没有注册触摸事件:

mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Log.e(TAG, "Hello world");
        return false;
    }
});

但是,当我执行以下操作时,我会注册触摸事件:

mInflatedLayout = getLayoutInflater().inflate(R.layout.my_layout, null, false);
mInflatedLayout.findViewById(R.id.child_view)
    .setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            Log.e(TAG, "Hello world");
            return false;
        }
});

注意:为简单起见,上面的代码是psuedocode-ish重写我的实际应用程序。请随意忽略任何可能导致编译时错误的内容。

我的问题是,为什么会这样?我愿意猜测这里有一些非常简单的东西,我很遗憾,但我对这件事情感到有些困惑。

编辑:这是我正在使用的布局文件。对于它的价值,视图本身将显示为ListView的空视图。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/header_view" />

<LinearLayout
    android:id="@+id/child_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:paddingRight="?baseGapSize"
    android:paddingLeft="?baseGapSize"
    android:orientation="vertical" >


    <CustomFrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_white_rounded_rect_inactive"
        android:paddingBottom="?baseGapSize"
        android:paddingTop="?baseGapSize" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textAppearance="@style/StandardLightText" />
    </CustomFrameLayout>

    <CustomRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_white_rounded_rect_inactive" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:contentDescription="@string/take_a_photo"
            android:paddingBottom="?baseGapSize"
            android:paddingTop="?baseGapSize"
            android:src="@drawable/camera_with_plus" />
    </CustomRelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的布局可能不会曝光 - 整个屏幕可能被子视图占用,因此触摸后面的布局永远无法触及..或 你可能有

android:clickable="true" //in childview

child_view.setClickable(true);

导致你的布局,而不是触发触摸事件。尝试删除它。

相关问题