【问题标题】:ViewPager with Toolbar and TabLayout has wrong height带有 Toolbar 和 TabLayout 的 ViewPager 高度错误
【发布时间】:2016-07-31 20:20:52
【问题描述】:

我在 AppBarLayout 下方有一个 ViewPager(带有一个工具栏和一个 TabLayout)。我不明白为什么加载的片段的高度超过可用空间,即使没有这么大的元素,使标签可滚动。

这是主要的布局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,但我猜它过度拉伸了 viewpager 的高度。

这里是设计视图中的布局:

【问题讨论】:

  • 你的应用可以截图吗?
  • @NguyễnTrungHiếu 是的,当然。添加了一个屏幕。
  • @Enrichman 你找到解决问题的方法了吗?问是因为我面临着完全相同的问题。 :(
  • @Droidwala nope,最后我稍微改变了设计.. :(
  • 看起来这是唯一的选择了..你在你的情况下做了什么设计更改?将注销移到其他地方?

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


【解决方案1】:

您可以尝试在 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>

【讨论】:

  • 这会有粘性操作栏的副作用,不会在滚动时隐藏。
【解决方案2】:

padding 添加到ViewPager 对我有用:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

请注意,内边距与工具栏height 的大小完全相同。
代替?attr/actionBarSize?actionBarSize 也是有效的。

【讨论】:

    【解决方案3】:

    试试这个。

    这里需要在Tab Bar中声明android:fillViewport="false"属性。

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

    请在此处更改Tab Bar

    <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="false" />
    

    编辑 1:

    这对我有用 请参考这个。

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            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:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="false" />
    
        </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" />
    
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 还是一样。正如你从截图中看到的那样,注销按钮应该在底部,但是片段的高度是更多的可用空间,所以所有的视图都是可滚动的。 (我添加了设计视图的屏幕截图)
    【解决方案4】:

    如果您需要不滚动的固定工具栏,那么您可以从布局设计中删除CoordinatorLayoutAppBarLayout 并改用RelativeLayout。这是支持库错误,到目前为止还没有解决方案。您可以简单地将以下布局代码用于ViewPager 以及ToolbarTabLayoutViewPager 在这种情况下不会溢出,因为它已被指出。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".HomeActivity">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            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:tabIndicatorColor="@android:color/white"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="?attr/colorPrimary"
            android:layout_below="@id/toolbar"/>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/tabs"/>
    
    </RelativeLayout>
    

    希望对某人有所帮助!

    【讨论】:

      【解决方案5】:

      我在 CoordinatorLayout 中使用了相对布局

      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/main_content"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              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|snap"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
      
              <android.support.design.widget.TabLayout
                  android:id="@+id/tabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:fillViewport="false" />
      
          </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_below="@id/appbar"
               />
      
      </RelativeLayout>
      </android.support.design.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案6】:

        您必须将 TabLayout 保留在 AppBarLayout 之外。

        类似的东西。

        <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.AppBarLayout>
        
        <android.support.design.widget.TabLayout
                android:id="@+id/home_tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/primary_blue_dark"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/primary_blue"
                app:tabTextColor="@color/primary_blue_light"
                app:tabTextAppearance="@style/tab_layout_font_style"/>
        
        <android.support.v4.view.ViewPager
                android:id="@+id/home_viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="fill_horizontal"/>
        

        这对我有用。

        【讨论】:

        • 错误答案,因为视图寻呼机没有行为
        【解决方案7】:

        对我来说,使用协调器布局的唯一解决方案是让 Viewpager layout_height 保持在限制范围内,将 layout_behaviour 添加到工具栏,如下所示:

         <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="?attr/colorPrimary"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:popupTheme="@style/AppTheme.AppBarOverlay">
        

        对你来说可能为时已晚,但也许它可以帮助别人。

        当我启动我的应用程序时,这并没有打扰我,但后来当我想在另一个选项卡上添加一个小吃栏时,只有当我滚动页面时,小吃栏才可见。

        【讨论】:

          【解决方案8】:

          我通过改变解决了这个问题

          android:layout_height="wrap_content"
          android:layout_height="<b>0dp</b>"

          和设置

          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintTop_toBottomOf="@id/tl_WelcomeTabs"

          在 ViewPager 的 XML 属性中。 0dp 在这种情况下意味着类似于 ma​​tch_constraint

          例如这个布局:

          <?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"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
          
          
              <android.support.design.widget.TabLayout
                  android:id="@+id/tl_WelcomeTabs"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="?attr/colorPrimary"
                  android:elevation="6dp"
                  android:minHeight="?attr/actionBarSize"
                  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
          
                  app:layout_constraintLeft_toLeftOf="parent"
                  app:layout_constraintRight_toRightOf="parent"
                  app:layout_constraintTop_toTopOf="parent"
                  >
          
                  <android.support.design.widget.TabItem
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Start"
                      />
          
                  <android.support.design.widget.TabItem
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Mission"
                      />
          
              </android.support.design.widget.TabLayout>
          
              <android.support.v4.view.ViewPager
                  android:id="@+id/vp_welcomePager"
                  android:layout_width="wrap_content"
                  android:layout_height="0dp"
          
                  app:layout_constraintLeft_toLeftOf="parent"
                  app:layout_constraintRight_toRightOf="parent"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintTop_toBottomOf="@id/tl_WelcomeTabs"
                  />
          
          
          </android.support.constraint.ConstraintLayout>
          

          左图:android:layout_height=<b>"wrap_content"</b> 右图:android:layout_height=<b>"0dp"</b>

          tldr;

          改变

          android:layout_height="wrap_content"
          

          android:layout_height="<b>0dp</b>"

          并在 ViewPager 的 XML 属性中设置 layout_constraintBottom_toBottomOflayout_constraintBottom_toBottomOf

          【讨论】:

            【解决方案9】:

            覆盖视图寻呼机的行为,并将视图寻呼机添加到 this.layouts

            override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean =
                        super.onDependentViewChanged(parent, child, dependency).also {
                            layouts.forEach { it.setPadding(it.paddingLeft, it.paddingTop, it.paddingRight, it.top) }
                        }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2020-01-25
              • 1970-01-01
              • 2016-10-16
              • 2017-03-10
              • 1970-01-01
              • 1970-01-01
              • 2015-08-21
              相关资源
              最近更新 更多