2017-06-29 195 views
0

活動由底部導航欄和片段組成。該片段包含一個導航抽屜,我想要在底部導航抽屜上方可見。如何在使用導航抽屜的片段時在底部導航欄上添加導航抽屜?

什麼我是這樣的:

我想實現的是這個

我的片段於版圖

<android.support.v4.widget.DrawerLayout 
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:id="@+id/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<android.support.design.widget.CoordinatorLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    > 
    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay" 
     > 
     <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:local="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      local:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

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


<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:paddingTop="?attr/actionBarSize" 
    android:layout_gravity="start" 
    > 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/panels_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 
</android.support.design.widget.NavigationView> 

我的活動 -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
android:id="@+id/activity_main" 
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="com.example.harshitaneja.homie.MainActivity"> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/navigation_main" 
    android:animateLayoutChanges="true" 
    android:id="@+id/frame_layout"> 
</FrameLayout> 
<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation_main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="@color/colorPrimary" 
    app:itemIconTint="@color/nav_item_state_list" 
    app:itemTextColor="@color/nav_item_state_list" 
    app:menu="@menu/bottom_navigation_items"> 

</android.support.design.widget.BottomNavigationView> 
</RelativeLayout> 

我很抱歉,如果我做了任何錯誤,同時問這個問題,因爲這是我的第一個問題在這裏。謝謝

+0

顯示您的佈局XML代碼 – Sony

+0

@Sony加我想到了這一點的佈局 – Harshit

回答

1

將您的android.support.design.widget.BottomNavigationView移動到您的content_main。讓您的活動將只包含一個視圖組,分片將包含抽屜和bottomNavigationView這樣

<DrawerLayout> 
    <CoordintorLayout> 
     <AppBarLayout> 
     </AppBarLayout> 
     <LinearLayout 
      android:orientation="vertical"> 

      <include layout="@layout/content_main" /> 

      <android.support.design.widget.BottomNavigationView 
       android:id="@+id/navigation_main" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:background="@color/colorPrimary" 
       app:itemIconTint="@color/nav_item_state_list" 
       app:itemTextColor="@color/nav_item_state_list" 
       app:menu="@menu/bottom_navigation_items"> 

     </LinearLayout> 
    </CooorDinatorLayout> 
</DrawerLayout> 
+0

。但它需要在每個片段中添加底部導航欄。如果導航欄發生變化,則必須在所有片段中進行更改。沒有更清潔的解決方案嗎? – Harshit

+0

你可以爲你的導航做一個額外的佈局,然後包括你想要的任何地方,就像你添加主要內容並添加自定義導航,如果有任何改變 – Sony

+0

我試過了。但是,因爲在更換碎片時再次重繪所有內容,所以看起來不那麼流暢。所以我換了另一種方式,將導航抽屜添加到主要活動中,然後通過碎片對其進行處理。 無論如何非常感謝您的幫助。 – Harshit