2012-02-01 71 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