Android片段与以前的视图和按钮侦听器重叠

Android片段与以前的视图和按钮侦听器重叠,android,android-fragments,Android,Android Fragments,我有一个活动A,里面有一个片段A 活动A使用布局X,片段A使用布局A 布局图X的代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:co

我有一个活动A,里面有一个片段A

活动A使用布局X,片段A使用布局A

布局图X的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    android:id="@+id/fragment1"
    android:name="android.app.DialogFragment"
    android:layout_width="wrap_content"
    android:layout_height="500dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="113dp"
    class="com.example.fragtester.FragA" />

</RelativeLayout>
我最终让布局B显示在布局A下

所以我使用FrameLayout将片段包装在布局X中,并使用

ft.replace(R.id.FrameLayout1, f);
现在视图运行良好。然而,另一个问题出现了

虽然布局B覆盖布局A,但按钮仍处于活动状态

这意味着,当我查看布局B时,我仍然可以单击布局A上的按钮,即使我没有看到它

甚至当我添加片段C/D/E时。。。。。(布局C/D/E…),布局A上的按钮仍处于活动状态

有人能解释为什么会这样吗?我是否错误地使用了片段?谢谢


通过的一种方法是使布局为空白,并使用其他布局覆盖它。但这似乎不是“正确”的方法???

不要在xml中包含片段,而是尝试为片段创建空容器。例如,空框架布局。然后以编程方式将片段放入其中。

删除片段并添加框架布局

   <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
    </FrameLayout>

然后以编程方式添加片段


在android fragment按钮中,单击pass through the fragments(我不知道这些片段是否应该像那样工作)。在这种情况下,我通常做的是让片段的布局可以点击。因此单击不会通过。

将以下属性添加到位于顶部的片段的XML根布局中

android:clickable="true"

这将确保触摸事件不会传播到顶层以外的地方。

我可以知道原因吗?在布局中添加片段和以编程方式添加片段有什么区别?谢谢你能再解释一下为什么会这样吗?谢谢
android:clickable="true"