在安卓一个Activity中,layout的左边是一个RelativeLayout包裹的listview,右侧是mapview,那么去点击listview的时候,去跳转,然后隐藏这个listview,这时地图右侧会有一个跟listview大小一样的黑屏,我猜测是隐藏listview的时候,mapview又往左边绘制了导致了,这个问题应该怎么解决?
时间: 2024-03-26 21:42:00 浏览: 22
这个问题可能是由于listview隐藏后导致mapview重新布局而导致的。你可以在listview的item点击事件中,先设置listview的可见性为GONE,然后再通过代码设置mapview的宽度为match_parent,这样可以保证在listview隐藏后,mapview会自动填充整个父容器,不会出现黑屏的情况。具体代码如下:
```
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 隐藏listview
listView.setVisibility(View.GONE);
// 设置mapview的宽度为match_parent
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
mapView.setLayoutParams(params);
}
});
```
相关问题
帮我写一个安卓layout文件,要求左边是listview,右边是百度地图的mapview
好的,以下是一个简单的安卓layout文件,满足您的要求:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 左边的ListView -->
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<!-- 右边的MapView -->
<com.baidu.mapapi.map.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
```
请注意,您需要在代码中实例化MapView并添加到您的活动中。还需要在清单文件中添加与百度地图相关的权限和服务声明。
但是这个RelativeLayout包裹的listview还是在左侧显示哈
对不起,我的回答有误。根据你的描述,实际上是想要在 `MapView` 上方覆盖一个 `RelativeLayout`,并在其中显示 `ListView`,对吗?那么可以使用 `android:layout_above` 属性将 `RelativeLayout` 放置在 `MapView` 上方,并使用 `android:layout_alignParentLeft="true"` 和 `android:layout_alignParentRight="true"` 属性将 `RelativeLayout` 左右两侧与父容器对齐,避免 `RelativeLayout` 在左侧显示。具体代码如下所示:
```
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/poi_list_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_list_circular"
android:visibility="gone"
android:layout_above="@id/bmapView">
<ListView
android:id="@+id/poi_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hardwareAccelerated="true"
android:scrollbars="none" />
</RelativeLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:hardwareAccelerated="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
```
这样可以将 `RelativeLayout` 放在 `MapView` 的上方,不会占用 `MapView` 的空间。