2015-12-22 30 views
0

我正試圖爲我的android應用程序實現非常標準的材料設計佈局。我有一個導航抽屜,選項卡和浮動操作按鈕。因此,我的每個片段都應該出現在佈局的中心,而不會重疊或推開任何障礙物。當我將我的FrameLayout與默認片段打開時,它將ActionBar(或標題欄,無論什麼)推到更高的位置並離開屏幕。我實際的java主要運行片段事務,不會混淆佈局。FrameLayout向上推出NavigationDrawer操作欄

如果我排除FrameLayout fragment_container那麼佈局就像正常一樣。

下面是截圖。這意味着要在標籤佈局上方閱讀「Material App」文本。

enter image description here

這是我的XML:

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<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_layout" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:fitsSystemWindows="true" tools:openDrawer="true"> 

    <include layout="@layout/app_bar_main" android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView android:id="@+id/nav_view" 
     android:layout_width="wrap_content" android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

tab_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     app:tabGravity="fill" 
     app:tabMode="fixed" 
     android:background="@color/colorPrimary" 
     app:tabIndicatorColor="@color/colorAccent" 
     app:tabSelectedTextColor="@color/white" 
     app:tabTextColor="@color/colorAccent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </android.support.design.widget.TabLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </android.support.v4.view.ViewPager> 

</LinearLayout> 

點fragment_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" tools:context="biz.kusasa.eventually.MainFragment"> 
</FrameLayout> 

app_bar_main.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:fab="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
    tools:context="com.material.demo.MainActivity"> 

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

    <com.github.clans.fab.FloatingActionMenu 
     android:id="@+id/menu1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:paddingRight="16dp" 
     android:paddingBottom="16dp" 
     android:elevation="2dp" 
     fab:menu_colorNormal="@color/colorPrimary" 
     fab:menu_colorPressed="@color/colorPrimaryDark" 
     fab:menu_colorRipple="@color/colorAccent" 
     fab:menu_labels_ellipsize="end" 
     fab:menu_labels_singleLine="true" 
     fab:menu_backgroundColor="#80d8ff" 
     fab:menu_fab_label=""> 

     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/fab1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_edit" 
      fab:fab_colorNormal="#43a047" 
      fab:fab_colorPressed="#388e3c" 
      fab:fab_colorRipple="#69f0ae" 
      fab:fab_size="mini" 
      fab:fab_label="@string/openCreate" /> 

     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/fab2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_add_white_48dp" 
      fab:fab_colorNormal="#43a047" 
      fab:fab_colorPressed="#388e3c" 
      fab:fab_colorRipple="#69f0ae" 
      fab:fab_size="mini" 
      fab:fab_label="Menu item 2" /> 

     <com.github.clans.fab.FloatingActionButton 
      android:id="@+id/fab3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_star" 
      fab:fab_colorNormal="#8e24aa" 
      fab:fab_colorPressed="#7b1fa2" 
      fab:fab_colorRipple="#ea80fc" 
      fab:fab_size="mini" 
      fab:fab_label="@string/join_group_label" /> 

    </com.github.clans.fab.FloatingActionMenu> 
    <include layout="@layout/content_main" /> 
    </android.support.design.widget.CoordinatorLayout> 

建議?

+0

如果你可以再嘗試在Android Studio中模板提供的標籤活動和實現使用它的代碼 –

+0

甚至在我開始使用選項卡之前,我都遇到了這個問題。這似乎是與框架佈局 –

+0

嘗試從框架佈局中刪除線性佈局 –

回答

0

改變你activity_main.xml中像這樣(保持app_bar_main內):LinearLayout中

<?xml version="1.0" encoding="utf-8"?> 
<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_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 
+0

這並沒有幫助不幸。 –

0

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<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_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="true"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     <include 
      layout="@layout/app_bar_main" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

    </android.support.v4.widget.DrawerLayout> 
+0

這是一個隱藏的標籤佈局,我不能在片段之間滑動。 –

+0

@ barnacle.m你的app_bar_main.xml是什麼樣的? –

+0

我現在將它發佈 –