【问题标题】:How to hide layout when clicking the are outside it单击外部时如何隐藏布局
【发布时间】: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


【解决方案1】:

试试这个可能对你有帮助

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 但是,如果我在 rel1 上添加 onclick,然后如果单击 rel1 内的任何子项(所有子项都可单击),那么单击事件会同时转到 rel1 和子项单击吗?
    【解决方案3】:
     <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
     }
    

    查看.GONE - 一切都被删除,空间可用于其他小部件 View.INVISIBLE - 一切都将被删除,但其他假发无法使用空间

    【讨论】:

    • 我不明白。当我点击 rel1 时,你想让我隐藏 rel1 ???我想在点击 rel1 时隐藏 rel2
    【解决方案4】:

    使第一个 relativelayout 可点击并在第一个添加 click listener 它将隐藏第二个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 2013-11-22
      • 2020-05-15
      • 2011-09-20
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多