我想构建一个折叠工具栏,其中的视图动态添加到CollapsingToolBarLayout。所以我需要设置AppBarLayout wrap_content
。
然后我创建了一个手机& android studio 2.2中的平板电脑模块并选择"滚动活动"。 我有一个这样的页面:
然后我将AppBarLayout的高度从@dimen/app_bar_height
更改为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>
现在页面如下所示:
ToolBar由系统状态栏覆盖。它看起来像fitSystemWindow =&#34; true&#34;不起作用。
我将fitSystemWindow更改为&#34; false&#34;在AppBarLayout上,如果CollaspingToolBarLayout中只有一个子节点,它看起来像是有效的。但是,当我向CollapsintToolBarLayout添加imageView时,状态栏背景不透明。
<?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>
我需要做的是:
有人帮我吗?
答案 0 :(得分:1)
覆盖CollapsingToolbarLayout.onMeasure(),必要时将status_bar_height
添加到布局高度:
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;
}
}