保证金仍然存在于替换的片段中

Margin persists in replaced fragment

我在 activity(main activity) 中所做的是用片段替换布局(线性布局 id/main_layout)。 main_layout 有额外的子布局,我对其应用了一些边距。在我应用边距并用新片段替换 main_layout 之后,应用的边距将我的整个片段视图向下推。如果我删除边距,片段将按照我的预期放置。

问题:为什么在主布局中应用到某些子元素的边距会保留到我替换的新片段中?更好的方法来做到这一点?

删除 3 android:layout_margin="10dp" 修复了我的片段,但我失去了相当主要的 activity

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

<ExpandableListView
    android:id="@+id/navdrawer"
    android:layout_width="@dimen/navdrawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:drawSelectorOnTop="false"></ExpandableListView>

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

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



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:elevation="5dp"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:background="@color/white"
            android:id="@+id/product1_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:id="@+id/product1_imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:elevation="5dp"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:background="@color/white"
            android:id="@+id/product2_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:elevation="5dp"
            android:background="@color/white"
            android:id="@+id/product3_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</LinearLayout>

我如何用片段替换布局

mCurrentFragment = new UserStoryFragment();
                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.main_layout, mCurrentFragment, mCurrentFragment.getClass().getName()).addToBackStack(mCurrentFragment.getClass().getName()).commit();

在主要 activity 下方,突出显示 main_layout

已替换的片段具有明显的不良边距

首先,我尝试替换作为 main_layout 父级的线性布局,但根本没有替换片段。将此父线性布局更改为框架布局后,一切似乎都有效。

这解决了我的问题

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

<ExpandableListView
    android:id="@+id/navdrawer"
    android:layout_width="@dimen/navdrawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:drawSelectorOnTop="false"></ExpandableListView>

<FrameLayout
    android:id="@+id/main_layout_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:elevation="5dp"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:background="@color/white"
            android:id="@+id/product1_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:id="@+id/product1_imageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:elevation="5dp"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:background="@color/white"
            android:id="@+id/product2_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:orientation="horizontal"
            android:elevation="5dp"
            android:background="@color/white"
            android:id="@+id/product3_linear_layout"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/panda"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</FrameLayout>

                    fm.beginTransaction().replace(R.id.main_layout_parent, mCurrentFragment, mCurrentFragment.getClass().getName()).addToBackStack(mCurrentFragment.getClass().getName()).commit();