2014-05-19 142 views
3

我越来越疯狂的Android视图。我想要一个带顶部(A)的酒吧和一个酒吧的底部(C)与我的应用程序height="wrap_content"的布局。中间的完整剩余空间(B)应该是带有另一个布局或TextView或其他内容的内容区域。但我无法得到这个工作。我尝试了很多布局,但是当我对B执行android:layout_height="match_parent"时,C消失了。有任何想法吗?由于事先enter image description here布局与顶部酒吧,底部酒吧和剩余空间内容视图

回答

6

可以使用Android:layout_weight属性来实现这一点,但你的父容器必须是一个的LinearLayout,在我的例子中,我只使用的LinearLayout作为父视图的孩子,但你可以使用另一种类型的观:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/layout_a" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/layout_b" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/layout_c" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

</LinearLayout> 

如果你是好奇,想知道它是如何工作......这里是explanation,好运:)

+0

解决了这个问题。简单的答案,它的作品。 thx所有答案 –

+1

工作正常,在主线性布局更改后android:layout_height =“wrap_content”android:layout_height =“match_parent” – Duque

+0

已修复! :)谢谢@Duque –

0

把内容转换成一个RelativeLayoutalignParentTopalignParentBottom属性为内容Ç并且还belowabove相关属性内容,如下所示:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <!-- create content A and C before --> 
    <LinearLayout 
     android:id="@+id/A" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > </LinearLayout> 
    <LinearLayout 
     android:id="@+id/C" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical" > </LinearLayout> 
    <!-- create content B regarding the previous ids --> 
    <LinearLayout 
     android:id="@+id/B" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/A" 
     android:layout_above="@id/C" 
     android:orientation="vertical" > </LinearLayout> 
</RelativeLayout> 

您可以尝试更改B的高度与wrap_content而不是match_parent如果这不起作用。让我知道这是否有帮助。

+1

是的,这将工作,只是用相同的规格嘲笑了一个视图,它工作得很好。使用重量的 – OceanLife

0

使用重量:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/A" 
     android:layout_width="match_parent" 
     android:layout_height="50dp"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/B" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/C" 
     android:layout_width="match_parent" 
     android:layout_height="50dp"> 
     <!-- Your view here or replace FrameLayout --> 
    </FrameLayout> 

</LinearLayout> 

将高度设置为0有利于性能,因为重量无论如何都会改变它。

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/top_bar" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 
<TextView 
    android:id="@+id/middle_area" 
    android:layout_weight="14" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 
    <TextView 
    android:id="@+id/bottom_bar" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"/> 

android_layout_weight与LinearLayout中结合能为你做的伎俩。尝试上面的代码,并根据您的需要调整android_layout_weight。 在我的例子中,顶栏将占据屏幕的1/16,底栏将占屏幕的1/16,中间区域占屏幕的14/16。您可以将数字更改为您的自定义需求。