我有一个带有ViewPager和另一个LinearLayout的RelativeLayout。我已将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用作容器来避免这种情况,如下所示:

<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

07-25 23:30
查看更多