Java 调整大小';谷歌地图V2';取半个屏幕而不是整个屏幕
我现在正在做一个Android项目:我想做的是让屏幕的上半部分显示地图,另一半显示我稍后要写的信息 我的问题是,地图占据了整个屏幕,我尝试了不同的布局(相对、网格)并放大了另一个布局,但无论我使用什么,地图总是占据整个屏幕 这是我的密码:Java 调整大小';谷歌地图V2';取半个屏幕而不是整个屏幕,java,android,google-maps-android-api-2,google-maps-api-2,Java,Android,Google Maps Android Api 2,Google Maps Api 2,我现在正在做一个Android项目:我想做的是让屏幕的上半部分显示地图,另一半显示我稍后要写的信息 我的问题是,地图占据了整个屏幕,我尝试了不同的布局(相对、网格)并放大了另一个布局,但无论我使用什么,地图总是占据整个屏幕 这是我的密码: public class Map extends SherlockMapFragment implements IPage, View.OnClickListener { private GoogleMap googleMap; private Mar
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>
再一次,地图工作没有任何问题,但它需要整个屏幕,我真的不明白为什么。
此外,我正在使用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>
由于您只有两个视图,因此不必将它们包装到其他视图中
为片段
和文本视图
设置相同的权重是关键。它将为每个视图提供一半屏幕(高度为线性布局的方向垂直) 使用这个:-
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import android.annotation.SuppressLint;
//import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class Map_Check extends Fragment {
public Map_Check(){}
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_view, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(48.8146, 2.39525), 10);
map.animateCamera(cameraUpdate);
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
和map_view.xml:-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp">
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
我知道有点晚了,但自从我路过这里寻找问题的答案,我的想法如下:
看起来,保存MapFragment的线性布局占据了整个屏幕。将android:layout\u height更改为fill\u parent,而不是match\u parent
这里的区别是,匹配父视图使视图的高度占据父视图的所有高度。在您的情况下,父级是屏幕大小的线性布局
填充父对象实际上填充父对象的可用空间
对于您的示例,以下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" >
<fragment
android:id="@+id/fragment_map"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:weight="7">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/infotext">
</TextView>
</LinearLayout>
</LinearLayout>
谢谢你的帮助,我忘了提到我以前已经尝试过使用“layout_weight”了,但是没有用。我只是用你写的东西修改了我的xml,但是地图仍然占据了整个屏幕。谢谢你的帮助,我尝试了这段代码,但是地图仍然占据了整个屏幕:/@LostCid,在map_view.xml内的MapView中指定布局宽度和布局高度的大小…(如果不起作用,请指定LinearLayout内的大小)。它肯定会起作用,但大小应该根据您的空间而定……您可以发布您调用片段的活动的代码吗?match\u parent
和fill\u parent
在各个方面都完全相同fill\u parent
刚刚重命名为match\u parent
,以更准确地描述它的功能。正如你在这里看到的,两者都等于-1(因此是否决票)。谢谢,我的信息来源是我上过的大学的一个班级,显然我的教授错了。偶尔发现一些新的东西是很好的。
|----------------|
| |
| |
| |
| |
| Map |
| |
| |
| |
| |
|----------------|
| Text |
|----------------|