-2

私は1つのアクティビティと多数のフラグメントを持っています。CollapsingToolbarLayoutとフラグメントでタイトルを変更できない

私の断片の1つはフルスクリーンで、CollapsingToolbarLayoutです。 フラグメントマネージャによって.addによって追加されました。

私は私のフラグメント上のツールバー、ボタンなど、必要があります。 enter image description here

をしかし、このために私はこのような活動のためのツールバーを設定する必要があります。

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

しかし、その後、私はタイトルを変更することはできませんの活動の私はCollapsingToolbarLayoutを断片から取り除き、それにタイトルを設定したり、静的にするなど、さまざまな方法を試しました。また、findViewByIdからツールバーを設定しようとしました。そしてGoogleからの他の方法、しかし誰も助けない。

どうすればこの問題を解決できますか?私は、この断片に独自のツールバーを設定し、次に動的にタイトルを変更する必要があります。

答えて

0

は、私はいくつかの活動を持っている私のアプリでこの

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

フラグメントの外にタイトルを変更する必要があります。私が言ったように、私は断片をたくさん持っています。断片に応じてツールバーのタイトルを変更する必要があります。 –

+1

コードを表示してください –

+0

それは必要ではありません。私が言ったように、問題はフラグメントで自分のツールバーを設定してから、アクティビティでタイトルを変更できないためです。 –

0

を試してみてください。各アクティビティには私自身のツールバーがあります。ツールバーのアクティビティのタイトルは、次のように設定します。

(ツールバー)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"); 
関連する問題