如何设置折叠工具栏的背景与自定义行为以适应整个屏幕

时间:2016-05-13 13:57:17

标签: android android-layout android-toolbar android-collapsingtoolbarlayout android-layoutparams

我关注了好的回购,其中显示了如何制作折叠工具栏WhatsApp-ProfileCollapsingToolbar的自定义行为。

我不喜欢的是当工具栏下方的图片(工具栏的字体为白色)为白色时,工具栏不可见。所以我试图将工具栏的背景设置为某种颜色。

首先我添加到widget_header_view.xml android:background="@android:color/holo_red_light",现在我就像:

<?xml version="1.0" encoding="utf-8"?>
<com.anton46.whatsapp_profile.HeaderView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_light"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@android:color/white"
        android:textSize="@dimen/header_view_start_text_size"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/last_seen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textColor="@android:color/white" />


</com.anton46.whatsapp_profile.HeaderView>

activity_main.xml {I}已将app:contentScrim="?attr/colorPrimary"更改为app:contentScrim="@android:color/holo_red_light"

但是这个回购在WhatsappHeaderBehavior效果中使用边距就像是:

enter image description here

但我希望它像:

enter image description here

编辑1:

<{3}} https://stackoverflow.com/users/3436179/alexanderhttps://stackoverflow.com/a/37280227/2401535提出的带有填充的

解决方案没有用,因为那时&#34;浮动&#34;工具栏覆盖了这里的后退按钮: toolbar covers back button

2 个答案:

答案 0 :(得分:1)

您应该使用填充而不是边距。为此,编辑WhatsuppHeaderBehavior.java,如下所示:

private int mStartPaddingLeft;
private int mEndPaddingLeft;
private int mPaddingRight;
private int mStartPaddingBottom;


  @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
        shouldInitProperties();

        int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
        float percentage = Math.abs(dependency.getY()) / (float) maxScroll;
        float childPosition = dependency.getHeight()
                + dependency.getY()
                - child.getHeight()
                - (getToolbarHeight(mContext) - child.getHeight()) * percentage / 2;

        if (Math.abs(dependency.getY()) >= maxScroll / 2) {
            float layoutPercentage = (Math.abs(dependency.getY()) - (maxScroll / 2)) / Math.abs(maxScroll / 2);
            child.setPaddingRelative((int)(layoutPercentage * mEndPaddingLeft) + mStartPaddingLeft,0,0,0);
         }
        child.setY(childPosition);
        if (isHide && percentage < 1) {
            child.setVisibility(View.VISIBLE);
            isHide = false;
        } else if (!isHide && percentage == 1) {
            child.setVisibility(View.GONE);
            isHide = true;
        }
        return true;
    }


private void shouldInitProperties() {
        if (mStartPaddingLeft == 0) {
            mStartPaddingLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_left);
        }

        if (mEndPaddingLeft == 0) {
            mEndPaddingLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);
        }

        if (mStartPaddingBottom == 0) {
            mStartPaddingBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);
        }

        if (mPaddingRight == 0) {
            mPaddingRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);
        }

        if (mTitleStartSize == 0) {
            mTitleEndSize = mContext.getResources().getDimensionPixelSize(R.dimen.header_view_end_text_size);
        }

        if (mTitleStartSize == 0) {
            mTitleStartSize = mContext.getResources().getDimensionPixelSize(R.dimen.header_view_start_text_size);
        }
    }

在MainActivity中使用setBackground方法进行工具栏

@Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(offset) / (float) maxScroll;

        if (percentage == 1f && isHideToolbarView) {
            toolbarHeaderView.setVisibility(View.VISIBLE);
            toolbar.setBackgroundColor(yourColor);
            isHideToolbarView = !isHideToolbarView;

        } else if (percentage < 1f && !isHideToolbarView) {
            toolbarHeaderView.setVisibility(View.GONE);
            toolbar.setBackgroundColor(yourColor);
            isHideToolbarView = !isHideToolbarView;
        }
    }

答案 1 :(得分:0)

如果你不急于截止日期,那么请使用google standard collapsing工具栏。

http://antonioleiva.com/collapsing-toolbar-layout/