点击视图外部后,视图消失

5

我希望在点击视图外部时隐藏视图,例如:

<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"的container RelativeLayout。
我尝试获取frameLayout容器,当点击容器时检查relativelayout容器是否显示,如果显示则将其视图设置为gone。以下是我尝试使用的代码:
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);

        }

    }
});

以上内容是否有任何错误?

实际上,我在框架布局容器中也有地图,所以我期望当我点击地图时,相对布局容器应该消失。


ن¸؛FrameLayoutو·»هٹ android:clickable="true"هگژé‡چ试م€‚ - ρяσѕρєя K
我现在尝试了一下,仍然有同样的问题,我点击地图它就不隐藏。 - Naveen Kumar
2个回答

9

添加一个透明的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,需要给 coverView 设置 OnClickListener

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

coverViewcontainerGrid都设置为View.GONE


0

尝试在FrameLayout中将dependent focusability设置为false,它就会起作用。

问题在于FrameLayout没有获取到点击事件。

如果父视图是ViewGroup,则Descendent focus ability false可以让您禁用子视图中的点击事件。


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