2014-01-29 43 views
1

我有這樣的XML佈局工作這是FrameLayouts父佈局:保證金不是的FrameLayout和重量

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:weightSum="2" 
    android:padding="10dp" 
    android:clipToPadding="false" 
    android:clipChildren="false"> 

    <LinearLayout 
     android:id="@+id/home_card_row1" 
     android:layout_height="0dp" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:clipChildren="false" 
     android:orientation="horizontal" /> 

    <LinearLayout 
     android:id="@+id/home_card_row2" 
     android:layout_height="0dp" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:paddingTop="10dp" 
     android:clipChildren="false" 
     android:clipToPadding="false" 
     android:orientation="horizontal" /> 

</LinearLayout> 

這是在父佈局填充我的獨立單的FrameLayout如上圖所示:

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_gravity="top" 
    android:layout_height="fill_parent" 
    android:layout_width="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/book_corners" 
    android:paddingRight="6dp" 
    android:paddingBottom="6dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginBottom="5dp" 
    android:layout_marginRight="20dp" 
    android:clipToPadding="false" 
    android:clipChildren="false"> 

    <ImageView 
     android:layout_gravity="top" 
     android:id="@+id/home_item_bg" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/book2" 
     android:layout_marginLeft="-12px" 
     android:layout_marginTop="-12px" 
     android:cropToPadding="false"/> 

    <RelativeLayout 
     style="@style/single_book" 
     android:layout_gravity="top" 
     > 

     <TextView 
      android:id="@+id/home_item_badge" 
      style="@style/round_badge" 
      android:text="34" 
      /> 

     <TextView 
      android:id="@+id/home_item_big_badge" 
      style="@style/big_badge" 
      android:text="Updated 30 minutes ago" 
      /> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_alignParentBottom="true" 
      > 

      <TextView 
       android:id="@+id/home_item_title" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="This is the very cool title" 
       android:textSize="20sp" 
       android:textStyle="bold" 
       android:textColor="#FFFFFF" 
       android:layout_marginBottom="10dp" 
       /> 

      <TextView 
       android:id="@+id/home_item_subtitle" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="And this is some very nice description about content" 
       android:textColor="#FFFFFF" 
       android:textSize="15sp" /> 
     </LinearLayout> 

     <RelativeLayout 
      android:id="@+id/home_item_preloader" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginTop="-12px" 
      android:layout_marginLeft="-12px" 
      android:background="#333" 
      > 

      <ProgressBar 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:layout_centerInParent="true"/> 

     </RelativeLayout> 

    </RelativeLayout> 
</FrameLayout> 

然後會發生的是,當我用它的FrameLayout子元素膨脹該父級xml時,一切都很好,但是當我嘗試以編程方式從單獨的xml添加該FrameLayouts時,左/右邊距消失。
這是爲什麼發生?

+2

僅供參考,'sp'應該用於字體大小,'dp'應該用於尺寸。 –

+1

也提供了該代碼。你應該設置佈局參數,當你添加一個視圖編程 –

+0

你確定這是你的佈局?有兩個問題:1)命名空間聲明應該只在根元素上,2)FrameLayouts可能沒有被使用,因爲它們沒有ID。 –

回答

0

除了使用sp(規模像素),它用於字體大小,你需要如下使用dp(密度像素),它用於尺寸在FrameLayout

同樣爲所有的邊距和補哪裏您已使用sp而不是dp

<FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_gravity="top" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:layout_weight="0.5" 
     android:background="#fff" 
     android:paddingRight="6dp" 
     android:paddingBottom="6dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginRight="5dp" 
     android:clipToPadding="false" 
     android:clipChildren="false"> 
+0

它不會改變任何東西:/ – lukaleli

+0

@jimmyweb你能告訴我們截圖嗎? – GrIsHu

+0

好的,現在它在我以編程方式添加邊距時起作用。但是這實際上說我不能使用佈局中指定的參數並且需要再次設置它們:/ – lukaleli