将framelayout放置在充当碎片容器的协调布局中



这是活动的XML,它在Framelayout中保存片段

<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" />

进行交易时,当前活动的内容和新添加的框架被重叠。我在哪里放置Framelayout?我如何仅看到新添加的Framgnet的内容?

抽屉布局首先包含两个孩子,第二个是用于抽屉项目。因此,您的framelayout应该是导航抽屉的第一个元素。而且,如果您想将TableAut与ViewPager一起使用,然后将TableAut和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替换此片段。

最新更新