片段重叠和活动

时间:2018-05-23 12:21:52

标签: android fragment

我的MainActivity页面左侧有导航栏,导航栏内的操作全部在fragment。当我点击任何操作时,fragment会与我的MainActivity页重叠。

Screenshot

您可以看到其上有textbutton。 我该如何克服这个问题? 的代码

public void selectedItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragment = null;

    switch(menuItem.getItemId()){

        case R.id.checkIn:
            //Not yet complete
            break;

        case R.id.checkOut:
            //Not yet complete
            break;

        case R.id.applyOff:
            break;

        case R.id.reportBug:
            break;

        case R.id.manageProfile:
            fragment = fragment_manageProfile.class;
            /*
             * Here is the complete 1
             */
            break;

        case R.id.logout:
            break;

        default:
            break;
    }


    try{
        myFragment = (Fragment)fragment.newInstance();
    }catch(Exception e){
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentManager.beginTransaction()
            .replace(R.id.viewPage, myFragment).addToBackStack(null).commit();



    menuItem.setChecked(true);
    drawer.closeDrawers();
}

First_layout xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewPage"
android:background="@drawable/login_background">

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">


<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />


        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


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


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

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



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

</FrameLayout>

2nd_layout 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="start">

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

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

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

在我的onCreate方法中,我使用第二种布局来查看布局 setContentView(R.layout.2nd_layout);

3 个答案:

答案 0 :(得分:1)

在片段布局中添加此android:background="@android:color/holo_red_dark"

答案 1 :(得分:0)

尝试为NavitagionView设置背景颜色。进入你的xml做这样的事情

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

注意

FrameLayout让它的孩子重叠。这是他的行为。如果您不想要,则必须使用不同的布局。看there

答案 2 :(得分:0)

由于first_layout.xml中的include标记,可能会发生重叠:

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

删除该行并查看重叠是否停止。