Android 如何使视图的高度与嵌套滚动视图上方的空间相匹配?
我想要一个地图视图,它占据了一个持久的底部工作表(NestedScrollView)上面的所有空间,就像我已经拥有的一样,你可以在下图中看到 对于MapView,我使用的是android:layout\u height=“238dp”。我曾尝试将其替换为android:layout\u height=“match\u parent”,但随后地图视图的高度被拉伸,占据了整个屏幕。这是代码,如上图所示Android 如何使视图的高度与嵌套滚动视图上方的空间相匹配?,android,nestedscrollview,Android,Nestedscrollview,我想要一个地图视图,它占据了一个持久的底部工作表(NestedScrollView)上面的所有空间,就像我已经拥有的一样,你可以在下图中看到 对于MapView,我使用的是android:layout\u height=“238dp”。我曾尝试将其替换为android:layout\u height=“match\u parent”,但随后地图视图的高度被拉伸,占据了整个屏幕。这是代码,如上图所示 <?xml version="1.0" encoding="utf-8"?> <
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".SpotFormActivity"
tools:showIn="@layout/spot_form_master_layout">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/save_spot_form_basic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapview2"
android:layout_width="match_parent"
**android:layout_height="238dp"** />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/spot_form_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:paddingBottom="60dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
....
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
....
我不想使用android:layout_height=“238dp”,因为在屏幕更大的设备中,地图高度不会自动调整。有没有人有更好的建议?这是我大多数时候用来更改物品高度和it套件以适应不同设备的方法
首先获取设备的高度:
public static int getDaviceHeight(Activity activity){
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height=displayMetrics.heightPixels;
return height;
}
然后更改项目高度:
MapView mapView = (MapView) v.findViewById(R.id.mapView2);
mapView.getLayoutParams().height=(getDaviceHeight(this))/3;
//ofcourse you can change the number and devide the total height as much as you want