1

Ich habe Toolbar mit Koordinatensymbolleiste in meiner App, so dass ich toolbar beim Scrollen verstecken kann. Ich habe auch ViewPager drin. Also, wenn Benutzer nur scrollen Scrollleiste und Tabs bleiben an der Spitze. Aber in anderen Aktivitäten möchte ich nur Symbolleiste anzeigen, was ist der bessere Weg zu diesem?Wie benutze ich benutzerdefinierte Symbolleiste in der gesamten App

Ich teile mein Koordinator Layout unter

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/AppBarLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <android.support.v7.widget.Toolbar     
xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/orange" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:titleTextColor="@color/white"> 
</android.support.v7.widget.Toolbar> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/MyTabLayout" 
     style="@style/TabLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     app:tabTextAppearance="@style/AppTabTextAppearance"> 



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

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




    <android.support.v4.view.ViewPager 
     android:id="@+id/MyViewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/MyTabLayout" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     ></android.support.v4.view.ViewPager> 



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

Mögliche Duplikat [Android Layout: Ist wiederverwendbare Komponente UI möglich?] (Http://stackoverflow.com/questions/2289730/android-layout-is-reusable-component-ui-possible) –

Antwort

3

Erstellen Sie ein separate Layout nur für die Symbolleiste.

toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    android:background="@color/white" 
    app:layout_scrollFlags="scroll|enterAlways" 
    app:titleTextColor="@color/white"> 


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

Dann ist es jetzt überall in Ihrem Layout in.

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/AppBarLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 


     <include 
      layout="@layout/toolbar" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/MyTabLayout" 
      style="@style/TabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/toolbar" 
      app:tabTextAppearance="@style/AppTabTextAppearance"> 


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

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


    <android.support.v4.view.ViewPager 
     android:id="@+id/MyViewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/MyTabLayout" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager> 


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

Dank zahid bhai. – FaisalAhmed

+0

@FaisalAhmed Sie sind willkommen. –

0

Wenn Sie nur Toolbar in andere Aktivitäten anzeigen möchten, können Sie die Toolbar in jedem Wurzel Layout der jeweiligen Aktivität wie Linearlayout oder RelativeLayout hinzufügen (was immer Anzüge die Bedürfnisse Ihrer App).

+0

verwendet I es Hauptaktivität xml unter Verwendung aber es schließt alle Ansicht wie viewpager, tablayout ein – FaisalAhmed