【问题标题】:fragment overlapping and activity片段重叠和活动
【发布时间】:2018-05-23 12:21:52
【问题描述】:

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

你可以看到上面有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);

【问题讨论】:

  • 你需要什么?导航抽屉的背景?
  • 我想解决这个问题。我想从我的导航栏中显示我的fragmentActivity 以更好地查看。
  • 请发布您的侧片段布局 xml
  • 完成。请检查一下。
  • 您是否在片段布局中添加了背景?没有android:background="@android:color/holo_red_dark"的可以试试这个

标签: android fragment


【解决方案1】:

在你的片段布局中添加这个android:background="@android:color/holo_red_dark"

【讨论】:

  • 接受.. 谢谢。但是,iewPageframeLayout 它们有什么不同吗??
【解决方案2】:

尝试为您的 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

【讨论】:

  • 这会将左侧导航栏的颜色更改为红色..
  • 是的,你的问题是什么?我了解到您有一个透明的导航视图。请说明您需要什么
  • 我想以完美的方式查看fragment。我不希望fragmentactivity 重叠,你可以从我的截图中看到。我想fragment 活动背景应该是自定义背景颜色,这将是黑色
  • 所以尝试在片段 xml 中指定背景颜色
  • 是的,我做到了。你可以从第一个布局&lt;frameLayoutandroid:background="@drawable/login_background" 看到。但我没有显示..我认为它是重叠的片段和活动。 mayB 我的代码出了点问题。
【解决方案3】:

由于 first_layout.xml 中的包含标签,可能会发生重叠:

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

删除那条线,看看重叠是否停止。

【讨论】:

  • 但是如果我删除这一行,那么我的活动将是空的吗?
  • 不,它不会,但它只会在 Framelayout(first_layout.xml)中显示协调器布局。我重新创建了用例,当用于片段事务替换片段的框架布局不为空时,就会发生重叠。删除协调器布局应该会阻止重叠的发生。
  • no.. 我无法删除协调器或包含布局。如果我删除包含布局.. --> 我的回收站视图是空的,如果我删除协调员 --> 我的折叠和导航无法显示
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
相关资源
最近更新 更多