无论设备屏幕高度如何,如何设置ViewPager的高度?

时间:2017-09-30 15:54:03

标签: android android-layout android-viewpager

我在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跨越底部导航栏后面的全高。我希望我的Error:error: linker command failed with exit code 1 (use -v to see invocation) 如下所示

enter image description here

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

在坐标布局后使用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>