billy billy - 6 days ago 8
Android Question

AppBarLayout fitSystemtWindows=“true" does not work when layout_height is wrap_content

I want to build a collapsing Toolbar with views add to CollapsingToolBarLayout dynamically. so i need to set AppBarLayout

wrap_content
.

Then i create a phone & tablet module in android studio 2.2 and select "Scrolling Activity".
i got a page like this:

default scrolling activity

and then i changed the height of AppBarLayout from
@dimen/app_bar_height
to
wrap_content


<?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:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="demo.billy.com.test_design.ScrollingActivity">

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

<android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_scrolling" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>


now the page is like below:

wrap_content AppBarLayout

ToolBar is covered by system statusbar. It looks like fitSystemWindow="true" does not work.

i change fitSystemWindow to "false" on AppBarLayout, if there is only one child in CollaspingToolBarLayout, it looks like works. but, when i add an imageView to CollapsintToolBarLayout, statusbar background is not transparent.

<?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:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="demo.billy.com.test_design.ScrollingActivity">

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

<android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/image_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.5"
android:scaleType="centerCrop"
android:src="@drawable/img"
/>
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_scrolling" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" app:srcCompat="@android:drawable/ic_dialog_email" />

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


enter image description here

what i need to do is:


  1. ToolBar must not be covered by system statusbar

  2. CollapsingToolbarLayout may has only one child: ToolBar

  3. CollapsingToolbarLayout may has many children: ToolBar + ImageView + etc...



is there anyone help me?

Answer

my own solution:

override CollapsingToolbarLayout.onMeasure(), add status_bar_height to layout height if necessary:

public class MyCollapsingToolbarLayout extends CollapsingToolbarLayout {

    public MyCollapsingToolbarLayout(Context context) {
        super(context);
    }

    public MyCollapsingToolbarLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Toolbar toolbar = null;
        int otherHeight = 0;
        //calculate max visible toolbar height
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (view instanceof Toolbar) {
                toolbar = (Toolbar) view;
            } else if(view.getVisibility() != GONE) {
                otherHeight = Math.max(otherHeight, view.getHeight());
            }
        }
        if (toolbar != null) {
            int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
            int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
            int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
            int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
            final int paddingWidth = getPaddingLeft() + getPaddingRight();
            final int paddingHeight = getPaddingTop() + getPaddingBottom();
            int height = paddingHeight;
            LayoutParams lp = (LayoutParams) toolbar.getLayoutParams();
            final int marginWidth = lp.rightMargin + lp.leftMargin;
            final int marginHeight = lp.topMargin + lp.bottomMargin;
            toolbar.measure(
                    getChildMeasureSpec(widthMeasureSpec, paddingWidth + marginWidth, lp.width),
                    getChildMeasureSpec(heightMeasureSpec, paddingHeight + marginHeight, lp.height)
            );
            int toolbarHeight = toolbar.getMeasuredHeight() + marginHeight;
            height += toolbarHeight;
            height += getStatusBarHeight();
            if (height > otherHeight) {
                setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : getWidth()
                        , (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);

            }
        }
    }
    private int getStatusBarHeight() {
        Resources res = Resources.getSystem();
        int resId = res.getIdentifier("status_bar_height", "dimen", "android");
        if (resId > 0) {
            return res.getDimensionPixelSize(resId);
        }
        return 0;
    }

}