我想在该视图外部点击时隐藏视图,例如
<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视图可见。
所以我的要求是当我在容器外面点击时,我想隐藏容器RelativeLayout和id =“containerGrid”。
我试图获取framelayout容器,当点击该容器时检查是否显示了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);
}
}
});
在上面的事情中我是否有任何错误。
实际上我也在那个framelayout容器中有地图所以我希望当我点击地图时,relativelayout容器应该去。
答案 0 :(得分:8)
添加另一个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
设置为{{1} }}:
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
。
答案 1 :(得分:0)
尝试在framelayout中设置相关的可聚焦性为false,它将起作用
问题是框架布局没有获得点击事件
后代焦点能力false允许您在子视图中禁用单击事件(如果父级别为
)