1

Я сделал панель инструментов, используя панель инструментов координат в своем приложении, чтобы я мог скрывать toolbar при прокрутке. У меня также есть ViewPager. Поэтому, когда пользователь просматривает только скрытые панели инструментов, а вкладки придерживаются вершины. Но в другой деятельности я просто хочу отобразить панель инструментов, что является лучшим способом для этого?Как использовать пользовательскую панель инструментов во всем приложении

Я делюсь мой макет координатору ниже

<?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

Возможный дубликат [Макет Android: Является ли многоразовый компонент пользовательский интерфейс возможен?] (Http://stackoverflow.com/questions/2289730/android-layout-is-reusable-component-ui-possible) –

ответ

3

Создайте макет separate только для панели инструментов.

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> 

Тогда теперь его в любом месте в макете.

<?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

Спасибо zahid bhai. – FaisalAhmed

+0

@FaisalAhmed, пожалуйста. –

0

Если вы просто хотите, чтобы отобразить панель инструментов внутри других мероприятий, вы можете добавить панель инструментов в любой корневой макет соответствующей деятельности, как LinearLayout или RelativeLayout (который когда-либо костюмы потребности вашего приложения).

+0

Я это основная деятельность xml с использованием , но она включает в себя все виды, такие как viewpager, tablayout – FaisalAhmed

Смежные вопросы