【问题标题】:Fragment's layout is too high with TabLayout + AppBarLayout使用 TabLayout + AppBarLayout 时 Fragment 的布局过高
【发布时间】:2019-01-11 05:56:10
【问题描述】:

我为此苦苦挣扎了好几个小时,在 google 和 stackoverflow 上寻找解决方案。认为这是我的应用程序中的一些微不足道的错误,但最终制作了一个空项目并且也可以重现它。只需运行新项目并选择导航样式为“Action Bar Tabs (with ViewPager)”的“Tabbed Activity”

然后尝试将任何小部件放在片段布局的底部。我通过修改 fragment_main.xml 并添加:

android:layout_height="match_parent"
android:gravity="bottom"
android:textAlignment="center"

所以整个布局是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:gravity="bottom"
        android:text="aaaaaaa"
        android:textAlignment="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.support.constraint.ConstraintLayout>

在设计模式下一切看起来都很好:

但是当你运行应用程序时:

只有在您滑动操作栏以隐藏它时,您才会看到文本:

因此不可能在标签片段的底部设置小部件,甚至无法将某些列表/图片匹配到父级的高度,因为底部边缘将始终位于导航栏下方。

我发现的解决方法在这里: ViewPager with Toolbar and TabLayout has wrong height

第一个是将AppBarLayoutViewPager 放在LinearLayout 之间,但是当滚动 ViewPager 的内容时,我失去了隐藏操作栏的功能。第二个是在ViewPager 中添加android:paddingBottom="?attr/actionBarSize",但是当我隐藏操作栏时会出现间隙。真的没有解决办法吗?

【问题讨论】:

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


    【解决方案1】:

    我认为这是一种预期行为,因为向上滚动时 ActionBar 会被隐藏。在设计模式下,可以显示文本,因为它不显示 TabLayout。但是,当您启动应用程序时,它会使 TabLayout 膨胀,并且片段将位于其下方。所以它不像片段正在扩大或给你错误的高度。

    想象一下放置一个与可见字段高度匹配的 ImageView(从 TabLayout 下方到导航菜单的正上方)。当您从那里隐藏操作栏时,它将在底部有一个间隙,因为没有内容可以填充隐藏操作栏的空间,除非您在向上滚动时拉伸 ImageView,这将导致有线拉伸图像:/

    我能想到的一个可能的解决方案是,如果你想在片段底部添加一个视图,我会将操作栏填充设置为视图,并且当我滚动屏幕时,我将调整填充取决于滚动偏移,这样我就可以一直在底部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2017-04-19
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      相关资源
      最近更新 更多