【发布时间】:2016-06-16 06:42:10
【问题描述】:
我有一个RelativeLayout,它有两个子Relative Layouts。让我们称它们为rel1 和rel2。 Rel1 在顶部有一个标题。然后在中心的个人资料图片。底部有两个水平放置的按钮。
Rel2 有一个 listview 。 Rel2 在父布局的底部对齐。 Rel2 占据屏幕的 50%,最初是不可见的。 当我单击 rel1 中的一个按钮时,rel2 变为可见。 由于 rel2 占据了屏幕的一半,所以 rel1 的上半部分是可见的,而 rel1 的下半部分会被 rel2 覆盖。
我的要求是当我点击 rel1 的上半部分(rel2 没有覆盖)时,rel2 应该隐藏。
我该怎么做。我应该在 rel1 布局上添加 onclick 侦听器吗?如果我在 rel1 上添加 onclick ,当我单击 rel1 布局的子项时会发生什么。点击事件会转到其子级还是 rel1 ..
请帮忙。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" >
<RelativeLayout
android:id="@+id/rel1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="23dp"
android:paddingTop="12dp" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:singleLine="true"
android:text="Title"
android:textSize="20sp" />
<ImageView
android:id="@+id/center_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/Image"
android:contentDescription="center IMage" />
<LinearLayout
android:id="@+id/buttonView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:baselineAligned="false"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginRight="1dp"
android:background="@drawable/button1"
android:contentDescription="@string/str_avoid_warning"/>
<ImageButton
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="1dp"
android:background="@drawable/button2"
android:contentDescription="@string/str_avoid_warning"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/rel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:fadingEdgeLength="7dp"
android:overScrollMode="never"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:requiresFadingEdge="vertical">
</ListView>
</RelativeLayout>
</RelativeLayout>
【问题讨论】:
-
在其后面使用 match_parent 视图并为其添加点击监听器 :)
-
我也在考虑添加一个匹配父级透明布局,比如 rel3 ,并在 rel2 变得可见时使其可见,否则会消失并在其上添加 onclick 侦听器。只是不确定它是否是一个好主意,我的意思是它只是黑客。我想知道我是否能找到更好的解决方案
-
有一些令人毛骨悚然的解决方案,比如在所有视图上添加一个触摸监听器并收听它。但无形的布局非常整洁。或者你应该在对话框中显示布局
-
如果您在父视图和子视图上都有 onclick 监听器,那么操作系统将处理点击事件。首先,它会根据您点击的区域检查子点击监听器,如果它在那里,然后会发生子点击(在这种情况下,它不会搜索父点击)。如果子点击不存在,那么它将检查父点击监听器
-
@ViswanathLekshmanan 谢谢。它有帮助。我正在为点击事件添加透明布局。它工作正常。
标签: android android-layout android-linearlayout onclicklistener android-relativelayout