2016-08-12 23 views
3

我有一個摺疊的工具欄佈局,其中包含一個圖像和摺疊顯示工具欄的標題。我需要更改工具欄標題字體,所以我在工具欄佈局中添加了一個textview。現在我收到以下錯誤生成重複每當我摺疊工具欄。requestLayout()被CollapsingToolbarLayout錯誤地調用

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; 
      } 
     } 
    }); 

我只想用一箇中心圖像,其上崩潰將顯示標題的工具欄。標題將具有自定義字體。現在有沒有更好的方法呢?

回答

1

實際上,您可以爲摺疊和展開模式設置標題顏色,當工具欄處於摺疊狀態時,它們將在兩者之間轉換。

在你的情況,而不是手動處理摺疊/擴展和設置標題,你可以設置擴展的標題顏色爲透明和摺疊的標題顏色爲你最初想要的東西。

所以,現在擴展時,工具欄標題是不可見的,當摺疊時,工具欄標題是可見的。

+0

從哪裏可以更改展開/摺疊模式標題的顏色? –

3

我對我的onOffsetChangedListener有同樣的問題。爲了解決這個問題,我不得不將我的代碼封裝在UI線程上運行的runnable。 所以用你,你就必須做到以下幾點:

@Override 
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
     getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        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; 
        } 
       } 
     }); 

注意,在和的setTitle的setText引號之間的空間。我相信你需要一個空間或者它不會工作。此外,請確保在您的xml中,您的CollapsingToolbarLayout上有您的

app:titleEnabled="false" 

。希望解決的問題:)

+4

不,我還是看到了大量的原木! – MohanRaj

1

我有同樣的問題,而這個頁面給了我一個提示:here

我刪除了collapsingToolbarLayout.setTitle("Dashboard"); collapsingToolbarLayout.setTitle(" "); 和創建了兩個樣式styles.xml

<style name="CustomCollapsingCollapsed"> 
    <item name="android:textColor">@color/colorText</item> 
</style> 
<style name="CustomCollapsingExpanded"> 
    <item name="android:textColor">@color/transparent</item> 
</style> 

並在

中使用它們
<android.support.design.widget.CollapsingToolbarLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:contentScrim="@color/colorAccent" 
    app:layout_scrollFlags="scroll|exitUntilCollapsed" 
    app:expandedTitleTextAppearance="@style/CustomCollapsingExpanded" 
    app:collapsedTitleTextAppearance="@style/CustomCollapsingCollapsed" 
    > 

沒有日誌了...希望它有幫助!

-1
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
     boolean isShow = false; 
     int scrollRange = -1; 

     @Override 
     public void onOffsetChanged(final AppBarLayout appBarLayout, final int verticalOffset) { 
      if (scrollRange == -1) { 
       scrollRange = appBarLayout.getTotalScrollRange(); 
      } 
      if (scrollRange + verticalOffset == 0) { 
       if (mToolbarTitle.getVisibility() == View.GONE) { 
        mToolbarTitle.setVisibility(View.VISIBLE); 
        mCollapsingToolbarLayout.setTitle("暱稱"); 
        mToolbarTitle.setText("暱稱"); 
       } 
       isShow = true; 
      } else if (isShow) { 
       mToolbarTitle.setVisibility(View.GONE); 
       isShow = false; 
      } 
     } 
    }); 

。希望解決的問題

5

下面這段代碼爲我工作 上AppBarLayout添加postRunnable

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(""); 
         } 
        } 
       }); 
      } 
     }); 
0

onOffsetChanged()叫了這麼多次和Android拋出的警告因爲您正在請求佈局作爲的一部分toolbar_title.setText("Dashboard");。你應該做的是,使用一個布爾標誌,並且只在滿足條件時調用它一次,或者檢查工具欄文本視圖的VISIBILITY標誌,就像這樣。

  1. 工具欄佈局內CollapsingLayout

    < 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/ThemeOverlay.AppCompat.Light"> 
    
         <TextView 
          android:id="@+id/toolbar_title" 
          style="@style/Toolbar.Title" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:visibility="gone" 
          android:layout_gravity="center" /> 
    
        </android.support.v7.widget.Toolbar> 
    
  2. 下添加的TextView在您的活動或片段類中OnOffsetChangedListener()修改一些東西像下面的檢查。這將停止警告。

    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
        @Override 
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
        if (verticalOffset == toolbar.getHeight() - collapsingToolbarLayout.getHeight()) { 
    
         if (textView.getVisibility() != View.VISIBLE) { 
         textView.setVisibility(View.VISIBLE); 
         textView.setText(title); // show toolbar title 
         } 
        } else { 
         if (textView.getVisibility() != View.GONE) { 
         textView.setVisibility(View.GONE); // hide title bar 
         } 
        } 
        } 
    }); 
    

    }

相關問題