在该视图之外单击时“视图消失”

2023-12-23

我想在单击视图外部时隐藏视图,例如,

<Framelayout android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
  <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <ImageView
    android:id="@+id/imgButtonToOpenGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:src="@drawable/open_grid_img" 
    />

  <RelativeLayout 
    android:id="@+id/containerGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    >

    <Button 
        android:id="@+id/button1grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/favourites"
        />

    <Button 
        android:id="@+id/button2grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/recent"
        android:layout_toRightOf="@+id/button1grid"
        />

    <Button 
        android:id="@+id/button3grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/gridview"
        android:layout_toRightOf="@+id/button2grid"
        />
  </RelativeLayout>
 </FrameLayout>

我在我的应用程序中所做的是 oncreate 我隐藏带有 id="containerGrid" 的relativelayout 视图,并在单击 imageview 时使该relativeLayout 视图可见。

所以我的要求是,当我在该容器外部单击时,我想隐藏带有 id="containerGrid" 的容器relativelayout。

我尝试获取框架布局容器,当单击该容器时,检查是否显示相对布局容器,如果显示,则其视图消失。这是我尝试过的代码,

containerMap = (FrameLayout)findViewById(R.id.container);

containerMap.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        if(rltvContainer.isShown()){
            rltvContainer.setVisibility(View.GONE);

        }

    }
});

我在上面的事情上有什么地方出错了吗?

实际上,我在该框架布局容器中也有地图,因此我希望当我单击地图时,相对布局容器应该消失。


加上另一个coverView在地图片段前面是透明的

<Framelayout android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
  <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <View
    android:id="@+id/coverView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:background="#00000000"
    android:visibility="gone"
    />

  <ImageView
    android:id="@+id/imgButtonToOpenGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:src="@drawable/open_grid_img" 
    />

  <RelativeLayout 
    android:id="@+id/containerGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    >

    <Button 
        android:id="@+id/button1grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/favourites"
        />

    <Button 
        android:id="@+id/button2grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/recent"
        android:layout_toRightOf="@+id/button1grid"
        />

    <Button 
        android:id="@+id/button3grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/gridview"
        android:layout_toRightOf="@+id/button2grid"
        />
  </RelativeLayout>
 </FrameLayout>

当。。。的时候containerGrid已打开(这意味着它的可见性是View.VISIBLE,设置coverView的可见度View.VISIBLE.

如果你想关闭containerGrid,即您要点击外部containerGrid,您实际上点击了coverView, set OnClickListener to the coverView:

coverView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        if(rltvContainer.isShown()){
            rltvContainer.setVisibility(View.GONE);
        }
        coverView.setVisibility(View.Gone);
    }
});

两者都设置coverView and containerGrid to View.GONE.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在该视图之外单击时“视图消失” 的相关文章

随机推荐