java - 调整 'Google maps V2' 以占据屏幕的一半而不是整个屏幕

标签 java android google-maps-android-api-2 google-maps-api-2

我现在正在做一个 Android 项目:我想做的是让屏幕的上半部分显示 map ,另一半显示我稍后要写的信息。

我的问题是 map 占据了整个屏幕,我尝试了不同的布局(相对、网格)并展开了另一个布局,但无论我使用什么, map 总是占据整个屏幕。

这是我的代码:

public class Map extends SherlockMapFragment implements IPage, View.OnClickListener {

private GoogleMap    googleMap;
private Marker       marker;
ViewGroup            view;


public Map() {
    super();
}
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = (ViewGroup) inflater.inflate(R.layout.map_page, null);

    googleMap = getMap();
    googleMap.setMyLocationEnabled(true);
    marker = googleMap.addMarker(new MarkerOptions().title("Event").position(new LatLng(0, 0)));

    final LatLng latLng = new LatLng(48.8146, 2.39525);      

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
    marker.setPosition(latLng);

    return view;
}

和 xml:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1.0"
   android:orientation="vertical" > 

  <fragment
    android:id="@+id/fragment_map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />
</LinearLayout>

 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1.0"
   android:orientation="vertical" >

   <TextView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/infotext">          
   </TextView>

 </LinearLayout>
</LinearLayout>

再一次, map 工作没有任何问题,但它占据了整个屏幕,我真的不明白为什么。 此外,我正在使用 lib SherlockBar(这导致我在实现 map 时遇到一些问题)。

我非常感谢任何帮助,我在 Java/eclipse 上开发还是个新手,所以错误经常发生。谢谢!

最佳答案

如果你想把你的屏幕分成两个相等的部分,你只需要使用下面的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/fragment_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50" />

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="50"
        android:id="@+id/infotext" />          

</LinearLayout>

因为您只有两个 View ,所以您不必将它们包装到其他 LinearLayout 中。

FragmentTextView 设置相同的权重是关键。它将为每个 View 提供屏幕的一半(高度为 LinearLayout 的方向是垂直)。

关于java - 调整 'Google maps V2' 以占据屏幕的一半而不是整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654469/

相关文章:

java - struts 资源包属性文件未映射某些键

java - 如何使用 Java 重定向 shell 脚本的输出?

android - 转弯多点路线导航 Google Maps Intent on Android

android - GoogleMap.InfoWindowAdapter 中的 picasso 图像加载问题

java - 如何以管理员权限运行命令?

java - 如何在测试方法中使用expectedExceptionsMessageRegExp

android - 使用 Kotlin 协程替换 BLE 回调

Android禁用按钮

php - Symfony2 - 如何检查我们是否被移动设备调用

android - 从另一个 Activity 返回时,MapFragment 的 map 加载延迟