android - 带工具栏和 TabLayout 的 ViewPager 高度错误

标签 android android-viewpager android-toolbar android-tablayout

我在 AppBarLayout 下方有一个 ViewPager(带有一个 Toolbar 和一个 TabLayout)。我不明白为什么加载的 fragment 的高度超过可用空间,即使没有这么大的元素,使选项卡可滚动。

这是主布局 xml:

<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:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

正如您在我的 Fragment 中看到的那样,我有一个带有 layout_alignParentBottom 的 Button,但我猜它过度拉伸(stretch)了 viewpager 的高度。

screenshot

这里是设计 View 的布局:

enter image description here

最佳答案

你可以尝试在AppBarLayout和ViewPager之间加一个LinearLayout。这个对我有用。 :)

<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">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

</LinearLayout>

关于android - 带工具栏和 TabLayout 的 ViewPager 高度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535695/

相关文章:

java - 如何返回 DocumentSnapShot 作为方法的结果?

android - 如何在 viewpager 中使用 webview 的水平滚动?

android - 是否可以禁用 ViewPager 上的滚动

android - 如何刷新或更新 viewpager android 中的所有 fragment

android - 更改工具栏中的返回图标颜色

android - 如何使编辑文本 float 在工具栏和主布局的顶部

android - 如何为 Android 创建针对 API 级别 11 的 AVD

Android:如何使用 CursorAdapter 将 child 添加到我的自定义 ViewGroup?

android - 以编程方式更改 AppBarLayout 主题

android - 某些测试人员无法使用 Android 中的 Beta 测试应用程序