【问题标题】:Cannot change title with CollapsingToolbarLayout and fragments无法使用 CollapsingToolbarLayout 和片段更改标题
【发布时间】:2017-10-10 03:18:08
【问题描述】:
我有一个活动和很多片段。
我的一个片段是全屏的并且有CollapsingToolbarLayout。
它是由片段管理器.add 添加的。
我的片段上需要工具栏、按钮等:
但为此我需要为这样的活动设置工具栏:
((MyActivity)getActivity()).setSupportActionBar(toolbarFromFragment);
但在那之后我无法更改活动的标题。我尝试了很多方法,比如从片段中获取CollapsingToolbarLayout 并为其设置标题,或者使其成为静态等。我还尝试从findViewById 再次设置工具栏。和谷歌的其他方法,但没有人帮助。
我该如何解决这个问题?我需要为此片段设置自己的工具栏,然后动态更改标题。
【问题讨论】:
标签:
android
android-fragments
fragment
toolbar
android-collapsingtoolbarlayout
【解决方案1】:
在我的应用中,我有一些活动。在每个活动中,我都有自己的工具栏。我在工具栏中设置activity的标题如下:
((工具栏) findViewById (R.id.myToolbar))
.setTitle(myActivityTitle);
它正在工作。
【解决方案2】:
试试这个
((MyActivity)getActivity()).setTitleEnabled(true);
((MyActivity)getActivity()).setTitle("Your Title");
【解决方案3】:
在fragment_**.xml中添加以下内容
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".assign.AssignOrderActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:elevation="@dimen/appbar_elevation"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
app:title="@string/app_name"
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:titleEnabled="false"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_marginLeft="@dimen/list_toolbar_side_margin"
android:layout_marginRight="@dimen/list_toolbar_side_margin"
android:elevation="@dimen/appbar_elevation"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<Add your views here>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
在片段中(Java):
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)
rootView.findViewById(R.id.collapsing_toolbar);
collapsing_toolbar.setTitleEnabled(true);
collapsing_toolbar.setTitle("My Title");