-2

我有一个活动和大量的片段。无法更改标题与CollapsingToolbarLayout和片段

我的一个片段是全屏,并有CollapsingToolbarLayout。 片段管理器增加了.add

我需要的工具栏,按钮等,在我的片段: enter image description here

但对于这一点,我需要设置工具栏的活动是这样的:

((MyActivity)getActivity()).setSupportActionBar(toolbarFromFragment); 

但在那之后我不能改变标题的活动。我尝试了很多方法,比如从片段获取CollapsingToolbarLayout,并将其设置为标题,或将其设置为静态等。另外,我尝试从findViewById再次设置工具栏。从谷歌和其他方式,但没有人帮助。

我该如何解决这个问题?我需要将自己的工具栏设置为该片段,然后动态更改标题。

回答

0

试试这个

((MyActivity)getActivity()).setTitleEnabled(true); 
((MyActivity)getActivity()).setTitle("Your Title"); 
+0

我需要改变片段外的标题。正如我所说,我有很多碎片,而且我需要根据碎片更改工具栏的标题,现在在屏幕上。 –

+1

请显示您的代码 –

+0

这不是必需的。正如我所说,问题因为在片段中我设置了自己的工具栏,然后在活动中我无法更改标题。 –

0

在我的应用程序我有几个活动。在每个活动中,我都有自己的工具栏。我将工具栏中的活动标题设置如下:

((Toolbar)findViewById(R.id.myToolbar)) .setTitle(myActivityTitle);

它正在工作。

+0

你可以阅读这个问题吗?你的答案是无用的。 –

0
Add the below in 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> 

In the fragment (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");