将FrameLayout放置在作为Fragment容器的Coordinator Layout中

时间:2017-04-28 07:43:04

标签: android android-fragments

这是在FrameLayout

中保存片段的活动的XML
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="enterAlways|scroll"
            app:theme="@style/ToolBarStyle">


            <ImageView
                android:id="@+id/action_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:padding="10dp"
                android:src="@drawable/ic_menu_white_24dp" />


            <veeresh.droid.harmony.utilities.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/app_name"
                android:textAllCaps="true"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                app:font_name="bold" />


            <ImageView
                android:id="@+id/action_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:padding="10dp"
                android:src="@drawable/ic_search_white_24dp" />

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


        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="3dp"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@color/primary_light" />

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


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

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


<android.support.design.widget.NavigationView
    android:id="@+id/nvView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimary"
    app:headerLayout="@layout/nav_header" />


<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

当进行交易时,当前活动的内容和新添加的framgent重叠。我在哪里放置FrameLayout? 我怎么只看到新添加的framgnet的内容?

1 个答案:

答案 0 :(得分:0)

抽屉布局只包含两个孩子,第一个是内容,第二个是抽屉物品。所以你的framelayout应该是导航抽屉的第一个元素。 如果你想在tabpager中使用tablayout,那么将tablayout和viewpager代码放在单独的片段中,并在你的活动加载时打开它。

所以您的抽屉布局xml应该如下所示

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusableInTouchMode="true">

    <FrameLayout  //Replace that fragment that should contain tablayout and viewpager with this framelayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    //Layout that contains drawer items

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

在你的片段的xml中放了这段代码

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="enterAlways|scroll"
            app:theme="@style/ToolBarStyle">


            <ImageView
                android:id="@+id/action_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:padding="10dp"
                android:src="@drawable/ic_menu_white_24dp" />


            <veeresh.droid.harmony.utilities.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/app_name"
                android:textAllCaps="true"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                app:font_name="bold" />


            <ImageView
                android:id="@+id/action_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:padding="10dp"
                android:src="@drawable/ic_search_white_24dp" />

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


        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="3dp"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextColor="@color/primary_light" />

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


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

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

现在你所要做的就是将这个片段替换为可以帮助你的framelayout..Hope