2010-12-01 211 views
1

我剛剛開始Android,並已陷入困境。我做了一個簡單的應用程序/視圖。這個想法是在頂部畫線迷宮,底部有動作按鈕。我的應用程序只在框架中繪製了一對x,但有效。問題在於我的按鈕被拉得太低,並且高度被限制。嵌套佈局的問題

我認爲問題都在我的佈局XML:

<?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"> 

     <FrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" > 

      <none.maze.MazeView 
      android:id="@+id/maze" 
       android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         /> 

      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 
       <TextView 
       android:id="@+id/text" 
        android:text="@string/maze_layout_text_text" 
        android:visibility="visible" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:gravity="center_horizontal" 
        android:textColor="#ff8888ff" 
        android:textSize="24sp"/> 
      </RelativeLayout> 

     </FrameLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" >    

      <Button 
       android:id="@+id/left" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Left" 
       android:layout_weight="1" 
       /> 

      <Button 
       android:id="@+id/forward" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Forward" 
       android:layout_weight="1" 
       /> 

      <Button 
       android:id="@+id/right" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Right" 
       android:layout_weight="1" 
       /> 

     </LinearLayout> 

</LinearLayout> 

回答

1

我認爲你不應該同時使用layout_height="fill_parent"layout_weight。嘗試爲FrameLayoutlayout_height="wrap_content"設置layout_height="0dip"layout_weight="1",對於較低的LinearLayout。較低的LinearLayout不得具有layout_weight屬性。

+0

謝謝精靈,這對我很有用。 – 2010-12-01 19:53:11

0

使用Hierarchy Viewer調試佈局。它太酷了。

+0

這很酷,我會記住它。這次事情是在正確的地方,所以他們在觀衆中看起來是正確的。 – 2010-12-01 19:54:11

+9

請注意層次結構查看器不適用於生產版本(操作系統,而不是您的應用程序)。它會失敗,出現「hierarchyviewer:無法調試設備」消息。如果您的手機上沒有調試版本,則可以在仿真設備上使用它。 – 2010-12-19 17:02:38

0

您的整個佈局只是太臃腫和無效。您使用方式太多ViewGroup s。我建議閱讀關於RelativeLayout s,尤其是整個Layout Tricks系列。