RelativeLayout、Framelayout 两布局重叠,使下层不响应点击事件

RelativeLayout、Framelayout 两布局重叠,使下层不响应点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
代码段A
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:scrollbars="none" />




<LinearLayout
android:id="@+id/search_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="gone"
android:orientation="vertical"
android:background="@android:color/darker_gray">

</LinearLayout>

</RelativeLayout>

FrameLayout、RelativeLayout,代码中控件代码的的位置越靠后,此控件越会显示在层叠布局的上方。

代码段B的布局会动态加载到代码段Asearch_container中,
并且形成重叠布局,此时 markView 会遮盖listView,但是点击事件会穿透markView传递给listView。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
代码段B
<?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="vertical" >

<View
android:id="@+id/markView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:background="@color/black" />

</LinearLayout>

怎样解决这个问题呢?

代码段B 增加一行代码
android:clickable="true"

变成这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
代码段C
<?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:clickable="true"
android:orientation="vertical" >

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:background="@color/black" />

</LinearLayout>

Fork me on GitHub