我正在尝试创建宽高比为1:1的Google地图,因此高度与宽度相同。这是我目前的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/border"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Locatiegegevens"
android:textStyle="bold" />
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Woonplaats:" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapPreview"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
map:cameraZoom="13"
map:liteMode="true"
map:mapType="normal" />
</LinearLayout>
片段类:
public class CaseFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
private Case caseObj;
private TextView title;
public CaseFragment() {
// Required empty public constructor
}
public static CaseFragment newInstance(Case caseObj) {
CaseFragment caseFragment = new CaseFragment();
Bundle args = new Bundle();
args.putSerializable("case", caseObj);
caseFragment.setArguments(args);
return caseFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
caseObj = (Case) args.getSerializable("case");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_case, container, false);
title = (TextView) view.findViewById(R.id.txtTitle);
title.setText(caseObj.getId());
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapPreview);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
我已经尝试使用LayoutParams
,但由于某种原因,宽度与高度相同:
ViewGroup.LayoutParams params = mMapFragment.getView().getLayoutParams();
params.height = params.width;
mMapFragment.getView().setLayoutParams(params);
答案 0 :(得分:1)
使用自定义视图类扩展MapView而不是片段
然后,覆盖onMeasure,如下所示
@Override
protected void onMeasure(int width, int height) {
// note we are applying the width value as the height
super.onMeasure(width, width);
}
它将解决您的问题。