布局如下
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expanded="false">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgTextActive"
app:contentScrim="@color/bgTextActive"
app:expandedTitleGravity="top"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:scrimAnimationDuration="0">
<include
layout="@layout/monthpicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
app:layout_collapseMode="pin">
<RelativeLayout
android:id="@+id/ll_date"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_date" 。 //--------------look here---------
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:drawableRight="@drawable/down_white"
android:drawablePadding="2dp"
android:textColor="@color/white"
android:textStyle="bold"
android:gravity="center"
android:text="2020-03"
android:textSize="18sp" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:visibility="gone"
android:id="@+id/iv_filter"
android:src="@drawable/unset_normal"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
一直刷日志报错:
requestLayout() improperly called by androidx.appcompat.widget.AppCompatTextView{bd68872 VFED..C.. ........ 396,0-639,138 #7f080188 app:
id/tv_date} during second layout pass: posting in next frame
2020-03-27 10:22:25.405 12191-12191/com.uwei.mrecord W/View:
solution
collapseLayout.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
int lastOffset = -1; //--------- look here -----
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (lastOffset == verticalOffset) return;
lastOffset = verticalOffset; //---------- and here -----
if (verticalOffset == -collapseLayout.getHeight() + toolbar.getHeight()) {
isOpenYear = false;
tvDate.setCompoundDrawables(null,null,drawableDown,null);
}else {
isOpenYear = true;
tvDate.setCompoundDrawables(null,null,drawableUp,null);
}
}
});
ok.
本文详细解析了在使用AppBarLayout时遇到的TextView布局重绘错误,并提供了有效的解决方案。通过添加OnOffsetChangedListener监听器,调整TextView的Drawable状态,避免了布局在第二轮布局过程中的不当调用。
1822

被折叠的 条评论
为什么被折叠?



