【问题标题】:How to set height of ViewPager regardless of device screen height?无论设备屏幕高度如何,如何设置 ViewPager 的高度?
【发布时间】:2017-09-30 15:54:03
【问题描述】:

我在MainActivity 中有一个标签布局,其中有ViewPager,我还在为MainActivity 添加底部导航,我希望ViewPager 的高度可以触及底部导航的顶部。

这是我的主要活动布局

 <android.support.design.widget.CoordinatorLayout 
     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"
     xmlns:tools="http://schemas.android.com/tools"
     android:orientation="vertical">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <TextView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10sp"
        android:paddingTop="10sp"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="40sp"
        android:text="@string/app_name" />



    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="top"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

     <include layout="@layout/bottom_nav"></include>

 </android.support.design.widget.CoordinatorLayout>

目前ViewPager 跨越底部导航栏后面的整个高度。我希望我的Viewpager 如下所示

【问题讨论】:

    标签: android android-layout android-viewpager


    【解决方案1】:

    使用LinearLayout 作为父布局,并为您的ViewPager 设置权重。它适用于你的情况。检查下面的代码。

     <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"
         xmlns:tools="http://schemas.android.com/tools"
         android:orientation="vertical">
    
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <TextView
            android:id="@+id/logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10sp"
            android:paddingTop="10sp"
            android:textAlignment="center"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="@string/app_name" />
    
    
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="top"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
    
         <include layout="@layout/bottom_nav"></include>
    
     </LinearLayout>
    

    【讨论】:

    • 这解决了问题,但我不确定我为什么首先使用 CoordinatorLayout 但谢谢。
    • @vikasdevde 如果它对你有帮助,那么请接受我的回答,以便对其他人有用。
    【解决方案2】:

    在坐标布局之后使用Liner布局作为父布局。

    <android.support.design.widget.CoordinatorLayout
          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:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        <TextView
            android:id="@+id/logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="10sp"
            android:paddingTop="10sp"
            android:textAlignment="center"
            android:layout_gravity="center"
            android:gravity="center"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:textSize="40sp"
            android:text="@string/app_name" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="top"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
    <include layout="@layout/bottom_nav"></include>
    </LinearLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多