我将AlertDialog与几个自定义布局一起使用,但其中一个存在一个奇怪的问题: 我正在尝试在AlertDialog中使用Google地图视图:
// this common code used for all alert dialogs
// I have different dialogIds so specific code is used where needed.
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
final View view = viewInitializer.getView(dialogId, mActivity);
builder.setMessage(message);
builder.setCancelable(false);
builder.setView(view)
// Add action buttons
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do something....
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
Dialog dialog = builder.create();
builder.show();
.......
// map view specific code
final MapView mapView = view.findViewById(R.id.mapView);
MapsInitializer.initialize(mActivity);
mapView.onCreate(dialog.onSaveInstanceState());
mapView.onResume();// needed to get the map to display immediately
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
final GoogleMap gmap = googleMap;
gmap.setMinZoomPreference(12);
LatLng ny = new LatLng(42.6021767,23.4674597);
gmap.moveCamera(CameraUpdateFactory.newLatLng(ny));
}
});
...........
-----------布局模板:------------ //可以的对话框之一。
<?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:gravity="center_horizontal"
android:orientation="vertical">
<NumberPicker
android:id="@+id/seatsCountPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"/>
</LinearLayout>
// the dialog with map view that are not ok.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:weightSum="5">
<Spinner
android:id="@+id/pointPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/points"
android:gravity="top"
android:layout_weight="1"/>
<Spinner
android:id="@+id/pointPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/points"
android:gravity="top"
android:layout_weight="1"/>
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="0pt"
android:layout_weight="3"
android:gravity="top"/>
</LinearLayout>
其他对话框都可以 带地图的对话框没有按钮。我试图设置较小的尺寸和不同的Mapview权重,因为我认为按钮在地图后面。但仍然没有按钮。
有什么想法吗?
[已解决] 嗯,奇怪。如果我删除“ layout_weight”并仅使用固定值作为高度,那就可以了。现在,我只需要找到如何处理不同尺寸的屏幕。