2016-07-07 86 views
2

我有一個collapsingToolbarLayout,我需要對標題TextView做一些額外的設置。獲取摺疊工具欄佈局的標題textview

問題:如何獲得對該TextView的引用?

enter image description here

<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:id="@+id/review_coordinator_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.stack.overflow.reviewmaschine.ReviewMaschineActivity" 
    tools:ignore="MergeRootFrame"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/app_bar_height" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <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" 
      app:toolbarId="@+id/toolbar"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/detail_toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

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

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

    <FrameLayout 
     android:id="@+id/item_detail_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior = "@string/appbar_scrolling_view_behavior" 
     /> 

    <com.mego.smscloud.reviewmaschine.SaveFloatingActionButton 
     android:id="@+id/review_fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:backgroundTint="@color/colorPrimary" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_floppy_white_48dp" 
     app:borderWidth="0dp" 
     app:elevation="@dimen/fab_elevation" 
     app:rippleColor="#00ffff"/> 

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

編輯: 簡單地增加一個TextViewToolbar不作爲CollapsingToolbarLayout標題確實 enter image description here

回答

3

Toolbar類創建其子View S作爲需要,使TextView將不存在,直到你設置的標題爲Toolbar。完成之後,有幾個選項。

你可以遍歷Toolbar的孩子View秒 - 使用ViewGroup#getChildCount()getChildAt()方法 - 找TextView。如果Toolbar中沒有其他TextView,那麼標題將是唯一的。如果有其他人,你應該檢查文字與你設定的標題。

另一種選擇是使用反射來抓取標題TextView字段。例如:

private TextView getTitleTextView(Toolbar toolbar) { 
    try { 
     Class<?> toolbarClass = Toolbar.class; 
     Field titleTextViewField = toolbarClass.getDeclaredField("mTitleTextView"); 
     titleTextViewField.setAccessible(true); 
     TextView titleTextView = (TextView) titleTextViewField.get(toolbar); 

     return titleTextView; 
    } 
    catch (NoSuchFieldException | IllegalAccessException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 

你應該在做這個方法的返回之前做一個空檢查。

+0

我已將您的答案作爲解決方案進行檢查,因爲它會爲我返回正確的textview。不幸的是,我不能設置任何東西到textview。請參閱我的代碼:protected void onResume(){ mTitleTextView =(TextView)getTitleTextView(mToolbar); if(mTitleTextView!= null){mtitleTextView.setText(「BLJSDosdjif」); } super.onResume(); } – jublikon

+0

嗯,'CollapsingToolbarLayout'將會在'Toolbar'上設置一些東西,就像標題一樣。如果您在'super.onResume()'調用後移動它,那麼該代碼可能會工作。稍後當我有機會時,我會看看'CollapsingToolbarLayout'的源代碼,但我還沒有使用它,所以我不確定它是如何處理的。 –

+0

我試圖在super.onResume()之後運行代碼。不幸的是它仍然不起作用。如果你有一點點看,我將非常感激。提前謝謝 – jublikon

0

這是Toolbar的默認標題是在同一個行爲整合如果您想更改標題,請撥打:

getSupportActionBar().setTitle(String title); 

但這只是改變文字。

但是,如果您想進行更多修改,請將您自己的TextView附加到Toolbar。這樣的:

<android.support.v7.widget.Toolbar 
      android:id="@+id/detail_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/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

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

然後隨便找稱號,你Activity

TextView title = (TextView) findViewById(R.id.title); 
+1

謝謝你的幫助。不幸的是,沒有給出可摺疊標題文本視圖的預期行爲。請看我編輯的帖子 – jublikon

+0

也許[這篇文章](http://stackoverflow.com/questions/26861843/add-custom-layout-to-actionbar-toolbar-with-no-margins)將幫助你增加重力元素。 –

相關問題