我想在该 View 之外单击时隐藏该 View ,例如,
<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 View ,并在单击imageview时使该relativeLayout View 可见。
所以我的要求是,当我在容器外部单击时,我想用id =“containerGrid”隐藏容器RelativeLayout。
我试图获取framelayout容器,并在该容器上单击时检查是否显示relativelayout容器,如果显示,则使其 View 消失。这是我尝试的代码,
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容器中也有 map ,因此在这里,我期望当我单击 map 时,relativelayout容器也应该去。
最佳答案
在 map fragment 的前面添加另一个透明的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
,将OnClickListener
设置为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
和containerGrid
都设置为View.GONE
。