2015-11-05 38 views
1

我在我的活動(主要活動)中所做的是用片段替換佈局(線性佈局ID/main_layout)。 main_layout有額外的子佈局,我應用了一些邊距。在應用了邊距並將main_layout替換爲新片段後,應用的邊距將我的整個片段視圖向下推。如果我刪除邊緣的片段放置如何我的意圖。保證金在被替換的片段中持續存在

問題:爲什麼應用於主佈局中某些子元素的邊距會保留到我替換的新片段上?更好的方法來做到這一點?

取出3機器人: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突出顯示的主要活動下方

main activity

註明不良保證金

Replaced fragment with marked undesirable margin

回答

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