我在我的活动(主要活动)中正在做的是用片段替换布局(线性布局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的主要活动
替换了带有标记的不良边距的片段
答案 0 :(得分:2)
首先,我尝试替换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();