I have a collapsingToolbarLayout
and I need to do some additional setup to the title TextView
.
Question: How can I get a reference to that TextView ?
<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>
Edit: Simply adding a TextView
to the Toolbar
does not integrate it in the same behavior as CollapsingToolbarLayout
title does
The Toolbar
class creates its child View
s as needed, so that TextView
won't exist until you've set the title on the Toolbar
. After you've done that, there are a couple of options.
You could iterate over the Toolbar
's child View
s - using the ViewGroup#getChildCount()
and getChildAt()
methods - looking for a TextView
. If there are no other TextView
s in the Toolbar
, then the title will be the only one. If there are others, you should check the text against what you set as the title.
Another option is to use reflection to grab the title TextView
field. For example:
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;
}
You should, of course, do a null check before doing anything with this method's return.
This is the default title of the Toolbar
If you would like to change the title, then just call:
getSupportActionBar().setTitle(String title);
But that just changes the text.
However, if you would like to do more modifications append your own TextView
to the Toolbar
. Like that:
<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>
And then just find the title in you Activity
TextView title = (TextView) findViewById(R.id.title);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.