CoordinatorLayout + AppbarLayout + Viewpager不会调整子布局的大小

19
我在使用CoordinatorLayout和ViewPager时遇到了问题: enter image description here 布局无法正确调整大小。假设高度包含选项卡的高度。所以当我滚动到底部时会看到这个问题:

enter image description here

主布局代码:
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".activities.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:id="@+id/root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/content_main" />
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

选项卡布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <FrameLayout
        android:id="@+id/regularLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </FrameLayout>
</LinearLayout>

子布局代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:clickable="false"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="bottom"
        android:layout_marginRight="85dp"
        android:src="@drawable/ic_android" />

</RelativeLayout>

有什么想法吗?我看到了几篇文章,但是找不到问题所在。我需要子元素的高度必须是屏幕高度减去操作栏高度和选项卡高度。


似乎存在双倍适配窗口的填充问题。你能发一下你的布局吗? - GPack
@GPack请查看答案末尾的“子布局代码”链接。 - Gaston Flores
5个回答

14

使用CoordinatorLayout + AppBarLayout + ViewPager时需要考虑以下几点:

  • 将ViewPager放置在CoordinatorLayout中,但要放在AppBarLayout
  • ViewPager 应设置其行为为 app:layout_behavior="@string/appbar_scrolling_view_behavior"
  • TabLayout 放置在 AppBarLayout 内部
  • 您还可以在 AppBarLayout直接子项上设置 滚动标志
  • CoordinatorLayoutAppBarLayout上都设置 android:fitsSystemWindows="true" ,这样您的薄纱颜色就可以应用于状态栏

因此,您的最终布局可能如下所示(不包括您的子布局,因为我猜它是您的ViewPager页面):

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true">

        <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/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways" />

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


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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

以下是几个你一定要查看的资源:


哇,非常好的答案,但对我没有用:(1)我的viewpager在FrameLayout中,该FrameLayout在CoordinatorLayout中,(2)FrameLayout具有行为,因为所有布局都将在其中加载,(3)我将TabLayout放在AppBarLayout中,但不起作用,选项卡栏和操作栏的背景颜色相同[见](http://postimg.org/image/x5fhvh1vl/)(4)设置滚动标志和android:fitsSystemWindows也无效。 - Gaston Flores
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Behzad Bahmanyar
我尝试了Chris Banes的示例,但它没有起作用,仍然无法正确计算高度。 - Gaston Flores
1
嘿,楼主能否将此标记为已接受的答案?这绝对有效。为逐行指令点赞。 - ralphgabb
1
将ViewPager放置在CoordinatorLayout内,但在AppbarLayout外部。这个建议解决了我的问题。 - mDonmez
显示剩余2条评论

5

我曾经遇到过类似的问题,解决方法是在布局中添加以下代码:

android:layout_marginBottom="?attr/actionBarSize

同时,在布局中加入以下代码:

app:layout_behavior="@string/appbar_scrolling_view_behavior"


示例:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="@android:color/holo_green_dark"
    app:srcCompat="@android:drawable/ic_input_add" />

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

3
您可以在子布局中使用 NestedScrollView
<android.support.v4.widget.NestedScrollView
    android:id="@+id/nest_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    android:fillViewport="true"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <your layout>

</android.support.v4.widget.NestedScrollView>

我尝试过这个,但还是不起作用,使用nestedScrollView允许我滚动到底部,但我的问题是其他的,我需要子元素调整到正确的高度。 - Gaston Flores

2
强制执行pager的requestLayout()方法有助于解决此问题。"Original Answer"翻译成"最初的回答"。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mViewPager.post(new Runnable() {
        @Override
        public void run() {
            mViewPager.requestLayout();
        }
    });
}

1
我会建议一个链接来解决设计问题:

查看教程

使用主要布局如下:
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:tabGravity="fill"
            android:theme="@style/ThemeOverlay.AppCompat.Dark" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_done"/>

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

我已经下载并编辑了一段代码,但它对我来说不起作用。我需要使子项正确调整大小。例如,我有两个选项卡,第一个选项卡具有简单布局,第二个选项卡具有RecyclerView。 - Gaston Flores
@GastonF,请按照完整的教程实现,跟随步骤操作,您将解决您的问题。您应该使用子布局match_parent。 - Dinesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接