【问题标题】:Margin persists in replaced fragment被替换片段中的边距仍然存在
【发布时间】:2015-11-06 03:44:25
【问题描述】:

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

问题:为什么在主布局中应用于某些子元素的边距会持续到我替换的新片段?更好的方法?

删除 3 android:layout_margin="10dp" 修复了我的片段,但我失去了漂亮的主要活动

<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();

在 main_layout 突出显示的主要活动下方

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

【问题讨论】:

    标签: android android-layout android-fragments margin


    【解决方案1】:

    首先我尝试替换作为 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多