2012-02-01 45 views
4

之間伸展的佈局,這是我的佈局(圖):安卓:兩個固定LinearLayouts

http://i.imgur.com/j8sqo.jpg


我想要做的就是第一次的LinearLayout 50dip高(這已經是OK)什麼,然後將第三個LinearLayout從底部向上調高50dip - 然後讓第二個LinearLayout填充剩下的位置。我該怎麼做?


這是我的XML:

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" > 

</LinearLayout> 


<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/linearLayout1" 
    android:orientation="vertical" > 

    <ViewFlipper 
    android:id="@+id/viewFlipper1" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/rain1" /> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/thunder1" /> 

    </ViewFlipper> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/linearLayout2"> 

</LinearLayout> 

</RelativeLayout> 

回答

16

總結所有三種佈局垂直線性佈局,並使用「1」的layout weight在中間佈局:

<LinearLayout 
    . 
    . 
    . 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     . 
     . 
     . 
     android:layout_height="50dip"> 
    <LinearLayout 
     . 
     . 
     . 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    <LinearLayout 
     . 
     . 
     . 
     android:layout_height="50dip"> 
</LinearLayout> 
+0

謝謝,這工作完美。 – 2012-02-01 11:57:40

0
LinearLayout layout; 
layout.addView(child, 2); 
+0

在XML中不可能做到這一點嗎? – 2012-02-01 11:39:34

+0

你不能添加沒有代碼的額外元素。 – Yahor10 2012-02-01 11:44:28

0

嘗試:第二個

<Relativelayout> 
    <Linearlayout1> 
    <Linearlayout3> 
    <linearlayout2> 
</Relativelayout> 
+0

我試過這個,但結果是一樣的。我還想知道的是,爲什麼第三個佈局如此之大,如果高度設置爲50dip?! – 2012-02-01 11:43:19

1

轉換成LinearLayout中相對佈局之前把第三佈局。定義元素的相對位置。您的問題將得到解決

或者線性佈局中使用的重量。定義重1.0頂級父佈局的高度和寬度第二線性layout.and應FILL_PARENT

+0

謝謝,我以這種方式解決了這個問題。 – 2012-02-01 12:02:58

0

我卻沒有將您可繪製在此代碼,但我相信這應該解決您的問題:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" > 

</LinearLayout> 


<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/linearLayout1" 
    android:layout_above="@+id/linearLayout3" 
    android:orientation="vertical" > 

    <ViewFlipper 
    android:id="@+id/viewFlipper1" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 

    </ViewFlipper> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" > 

</LinearLayout> 


</RelativeLayout> 
+0

也謝謝。 – 2012-02-01 12:02:39

1

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:background="#FF00FF"  
     android:id="@+id/first"  
     /> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dip" 
     android:background="#0000FF" 
     android:id="@+id/last"  
     android:layout_alignParentBottom="true"   
     />   
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#FFFFFF" 
     android:layout_above="@id/last" 
     android:layout_below="@id/first"  
     > 
     <ViewFlipper 
      android:id="@+id/viewFlipper1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      >  
       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:src="@drawable/rain1" />   
       <ImageView 
        android:id="@+id/imageView2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:src="@drawable/thunder1" />  
     </ViewFlipper> 

    </LinearLayout> 
</RelativeLayout> 
+0

謝謝,我之前嘗試過,但是我得到了「循環依賴」的錯誤。也許我正在做別的事情。無論如何謝謝 – 2012-02-01 11:59:03

+0

這真的很奇怪,我在這裏沒有任何錯誤! – Hiral 2012-02-01 12:18:04

+0

也許我做了其他的錯誤:) 下次我會記住這一點。 – 2012-02-01 14:12:00