我有一个带有ViewPager 的RelativeLayout 和另一个LinearLayout。我已将 ViewPager 的布局宽度设置为 match_parent
并将布局高度指定为 250dp 的固定值。
但是,当屏幕尺寸越来越大时,ViewPager 的高度会变小。
如何让高度根据屏幕尺寸变化?
如果我将高度设置为wrap_content
,它几乎会占据整个屏幕,除了为其他布局保留的一小部分。
代码发布在下面。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
最佳答案
尝试使用这个
如果您想为多种设备进行屏幕设计,
您可以使用 WeightSum 和 LinearLayout 作为 Container 来避免这种情况,如下所示:-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context="com.example.sloid.destinations.navigationdrawerfragments.aboutSLFragment">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="@+id/SliderDots"
android:layout_below="@+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.5"
/>
</LinearLayout>
供引用:- https://developer.android.com/reference/android/widget/LinearLayout.html
关于java - 如何根据屏幕尺寸动态改变高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787219/