如何在单击时隐藏布局

时间:2016-06-16 06:42:11

标签: android android-layout android-linearlayout onclicklistener android-relativelayout

我有一个RelativeLayout,它有两个子相对布局。让我们称它们为rel1和rel2。 Rel1在顶部有一个标题。然后在中心的个人资料图片。底部有两个水平放置的按钮。

Rel2有一个列表视图。 Rel2在父布局的底部对齐。 Rel2占据屏幕的50%,最初不可见。 当我点击rel1中的一个按钮时,rel2变为可见。 由于rel2占用了屏幕的一半,因此rel1的上半部分可见,rel1的下半部分被rel2覆盖。

我的要求是当我点击rel1的上半部分(rel2不包含)时,rel2应该隐藏。

我该怎么做我应该在rel1布局上添加onclick监听器。如果我在rel1上添加onclick,当我点击rel1布局的子项时会发生什么。 click事件将转到其子项还是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>

4 个答案:

答案 0 :(得分:2)

试试这可能会对你有所帮助

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);
}

答案 1 :(得分:0)

将rel1和rel2点击添加android:clickable="true",然后将OnClickListener添加到rel1。

答案 2 :(得分: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 - 所有内容都被删除,Space可用于其他小部件 View.INVISIBLE - 一切都将被删除,但其他wigets没有空间

答案 3 :(得分:0)

使第一个relativelayout可点击并首先添加clicklistener它将隐藏第二个

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