CoordinatorLayout wrap_contents does not work when you put a view in CoordinatorLayout

Asked 2 years ago, Updated 2 years ago, 54 views

Problems

The layout is made using CoordinatorLayout as shown below, but if you put View in CoordinatorLayout and outside AppBarLayout, the bottom of the View sticks out from CoordinatorLayout even though CoordinatorLayout is set to wrap_contents.(See image)

ViewPager will have scrollable Fragments.

If anyone knows the cause, please let me know.

hanging down

Tried

·I also placed a view other than ViewPager, but the result was the same
·I put it in AppBarLayout, but behavior doesn't work properly anymore
·I tried connecting AppBarLayout and View with LinerLayout, but behavior also didn't work

Code

<?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"
    android: id="@+id/FM_RootLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorBackground"
    android: orientation="vertical">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android: id="@+id/FM_MainContents"
        android:layout_width="match_parent"
        android:layout_height="wrap_contents" >

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorBackgroundZ1">

            <com.google.android.material.tabs.TabLayout
                android: id="@+id/FM_TabView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAllways"
                android:background="@color/colorBackgroundZ1"
                app:layout_constrainEnd_toEndOf="parent"
                app:layout_constrainStart_toStartOf="parent"
                app:layout_constrainTop_toTopOf="parent"
                android:layout_gravity="center"
                app:tabGravity="center"
                app:tabMode="scrollable"
                app:tabTextColor="@color/colorChar"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="@drawable/shadow_up"
                android:translationZ="3dp"
                app:layout_constrainEnd_toEndOf="parent"
                app:layout_constrainStart_toStartOf="parent"
                app:layout_constrainTop_toTopOf="parent"/>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.viewpager.widget.ViewPager
            android: id="@+id/FM_Pager"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constrainedHeight="true"
            app:layout_constrainBottom_toBottomOf="parent"
            app:layout_constrainEnd_toEndOf="parent"
            app:layout_constrainStart_toStartOf="parent"
            app:layout_constrainTop_toTopOf="parent"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <FrameLayout
        android: id="@+id/FM_Controller"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

android xml android-layout

2022-09-30 10:54

1 Answers

Well, I haven't been playing with CoordinatorLayout lately, so I'm sorry if I'm wrong.

This is probably normal (according to specifications).

ViewPager is fixed in height at 500dp, but assume that you want to place another View (for example, button strips, banner ads, etc.) in the space below.

In the case of CoordinatorLayout, when you scroll, the AppBar scrolls together and disappears, and at that stage, the Viewpager is in the 500dp full height state and scrolls in it.

Therefore, in the initial state, the height of the AppBar is normally hidden behind another View placed below.Therefore, the height of CoodinatorLayout deviates by AppBar in terms of layout, and the View added to the space below must be laid out from that height.

Also, by the way, it's not wrap_content but wrap_contentAlso, app:layout_constraint* is an attribute for View children of ConstraintLayout, so I think all of them are unnecessary.


2022-09-30 10:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.