android – requestLayout()被CollapsingToolbarLayout不正确地调用

我有一个折叠的工具栏布局,其中包含一个图像,并在折叠时显示工具栏标题.我需要更改工具栏标题字体,所以我在工具栏布局中添加一个textview.现在,每当我折叠工具栏时,我都会重复生成以下错误.
08-12 13:14:19.604 2263-2263/com.panoroma.admin W/View: requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{2d353cd6 V.ED.... ........ 0,0-1080,390 #7f0c0070 app:id/collapsing_toolbar} during second layout pass: posting in next frame 
08-12 13:14:19.604 2263-2263/com.panoroma.admin W/View: requestLayout() improperly called by android.support.v7.widget.AppCompatTextView{1bb84b57 V.ED.... ........ 168,48-407,119 #7f0c0073 app:id/toolbar_title} during second layout pass: posting in next frame

我的布局……

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enteralways|enteralwaysCollapsed|exitUntilCollapsed">

        <ImageView
            android:id="@+id/header"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp"
            android:background="@drawable/dashboard80"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.5"  />

        <android.support.v7.widget.Toolbar
            android:id="@+id/da_toolbar"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"
            android:layout_height="?attr/actionBarSize">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="?attr/colorAccent"
                android:id="@+id/toolbar_title"/>

        </android.support.v7.widget.Toolbar>

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


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rel_dash_icon"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

.........................

</ScrollView>

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

java文件..

Typeface ubuntuC = Typeface.createFromAsset(getAssets(),"ubuntuC.ttf");
Toolbar toolbar = (Toolbar) findViewById(R.id.da_toolbar);
    toolbar.setTitle("");
    setSupportActionBar(toolbar);

    toolbar_title = (TextView)toolbar.findViewById(R.id.toolbar_title);
    toolbar_title.setTypeface(ubuntuC);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha,null));
    else
        getSupportActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    getSupportActionBar().setdisplayHomeAsUpEnabled(true);

    final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = false;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout,int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
//                    collapsingToolbarLayout.setTitle("Dashboard");
                toolbar_title.setText("Dashboard");
                isShow = true;
            } else if(isShow) {
//                    collapsingToolbarLayout.setTitle("");
                toolbar_title.setText("");
                isShow = false;
            }
        }
    });

我只想要一个带有中心图像的工具栏,在折叠时会显示标题.标题将具有自定义字体.现在,有更好的方法吗?

解决方法

以下代码我有
在AppBarLayout上添加了Runnable
mAppBar.addOnOffsetChangedListener(new AppBarStatechangelistener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout,final int state,int done) {
                mAppBar.post(new Runnable() {
                    @Override
                    public void run() {
                        if (state == COLLAPSED) {
                            mToolBarTitle.setText(model.getTitle());
                        } else if (state == EXPANDED || state == IDLE) {
                            mToolBarTitle.setText("");
                        }
                    }
                });
            }
        });

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...