点击布局外部时如何隐藏布局

3
我有一个RelativeLayout布局,其中包含两个RelativeLayout子元素,我们称它们为rel1和rel2。Rel1在顶部有一个标题,中间有一个简介图片,底部水平放置两个按钮。
Rel2有一个ListView,位于父布局的底部,Rel2占据了屏幕的50%,最初它是不可见的。当我点击rel1中的一个按钮时,rel2就会变得可见。由于Rel2占据了屏幕的一半,所以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视图,并为其添加点击监听器 :) - Viswanath Lekshmanan
我也在考虑添加一个match parent透明布局,比如rel3,并在rel2变为可见时使其可见,否则隐藏,并在其上添加onclick监听器。只是不确定这是否是一个好主意,我的意思是这只是一个hack。我想知道是否有更好的解决方案。 - Pardeep Kumar
有一些可怕的解决方案,比如在所有视图上添加一个触摸监听器并侦听它。但是不可见的布局非常整洁。或者你应该在对话框中显示布局。 - Viswanath Lekshmanan
如果您在父视图和子视图上都有onclick监听器,那么操作系统将负责点击事件。首先,它将根据您单击的区域检查子点击侦听器,如果存在,则会发生子点击(在这种情况下,它将不会搜索父点击)。 如果子点击不存在,则会检查父点击侦听器。 - Kumar M
1
@ViswanathLekshmanan 谢谢。这很有帮助。我正在为点击事件添加透明布局。它运行良好。 - Pardeep Kumar
4个回答

3

试试这个,它可能对你有帮助。

private RelativeLayout llRoot;
private RelativeLayout llContent;

llRoot = (RelativeLayout) findViewById(R.id.rel1);//in onCreate()
llContent = (RelativeLayout) findViewById(R.id.rel2);//in onCreate()

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Rect ContentBounds = new Rect();
    llRoot.getHitRect(ContentBounds);

    if (ContentBounds.contains((int) ev.getX(), (int) ev.getY())) {
        //Do Your stuff here

    }

    return super.dispatchTouchEvent(ev);
}

0
使rel1和rel2都可点击,添加android:clickable =“true”,然后将OnClickListener添加到rel1。

但是如果我在rel1上添加onclick,然后如果单击rel1内的任何子元素(所有子元素都可以单击),那么单击事件会同时传递到rel1和被单击的子元素吗? - Pardeep Kumar

0
 <RelativeLayout 
        android:id="@+id/rel1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="23dp"
        android:onClick="hideMe"
        android:clickable=true
        android:paddingTop="12dp" >


 public void hideMe(View view){
 ViewGroup viewgroup = (ViewGroup) getLayoutInflater().inflate(R.layout.rel2, null);
 viewgroup.setVisibility(View,GONE);//CHECK THE OTHER OPTION BELOW
 }

View.GONE -所有内容都被移除,空间可用于其他小部件 View.INVISIBLE -所有内容都被移除,但空间不可用于其他小部件


我不明白。你是想让我在点击rel1时隐藏rel1吗?我想在点击rel1时隐藏rel2。 - Pardeep Kumar

0

让第一个 RelativeLayout 可点击,并在其上添加 click listener,当点击时它将隐藏第二个。

 Rel1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {             
     if(Rel2.getVisibility()==View.VISIBLE) {
        Rel2.setVisibility(View.GONE);
      } 
}
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接