2011-10-28 112 views
0

我是Android新手,所以不太擅长GUI布局。我正在为我的应用程序构建一个GUI,但无法让它按照我想要的方式行事。我需要的是屏幕底部水平堆叠的4个按钮。在按钮上方放置了一个SurfaceView,我想填充屏幕的其余部分。结果应该是这样的(我希望这是明确的):
Android - 图形用户界面布局

----------------- 
-    - 
-    - 
-    - 
-    - 
- SurfaceView - 
-    - 
-    - 
-    - 
----------------- 
--- --- --- --- 
-B- -B- -B- -B- 
--- --- --- --- 

我得到的closeset是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 
      <SurfaceView 
       android:id="@+id/surfaceView1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" > 
      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
      </LinearLayout> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

不幸的结果是这样的:

----------------- 
- SurfaceView - 
----------------- 
--- --- --- --- 
-B- -B- -B- -B- 
--- --- --- --- 

回答

2
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


      <SurfaceView 
       android:id="@+id/surfaceView1" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight = "1" 
      /> 

      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
       <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp" 
        android:text="Button" /> 
      </LinearLayout> 

</LinearLayout> 

试试这个。

layout_weight是一个工作在线性布局的孩子上的标签。如果提到的话,孩子们会根据体重分享取向测量。在你的情况下,由于线性布局是垂直的,因此如果提到权重,他们将共享高度。

linearLayout的默认weightSum是1,所以如果你给任何一个孩子1,它会占用剩余的空间。假设父母有2个孩子,其体重为0.6和0.4,第一个孩子将占60%的父母身高,其余40%由第二个孩子。

+0

谢谢,这工作。只要网站允许,我会尽快接受。你可以请解释你如何使用这些属性:android:layout_height =“0dp” android:layout_weight =“1” – Yoav

+1

检查我编辑的答案。 –

+0

再次感谢 – Yoav

0

尝试对顶层布局使用相对布局,并将alignParentBottom用于按钮栏布局。