LinearLayout中的FrameLayout在View中被推到一边

时间:2014-12-24 19:42:57

标签: android button android-linearlayout imagebutton android-framelayout

我的应用程序中有一个游戏布局,它有四个按钮,应该显示在我的视图顶部,然后我有两个FrameLayout也在这个线性布局中。第一个FrameLayout只占视图的前1/4,所以我的imageButtons不允许在顶部实现拖放API。第二个FrameLayout用于视图的底部3/4,其中有4个图像按钮。

我的问题是,当显示游戏布局时,线性布局中的按钮会按照我想要的方式显示,但两个FrameLayout会被压缩到右上角。

以下是代码:

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

        <Button
            android:id="@+id/doneButton"
            android:layout_width="170dp"
            android:layout_height="wrap_content"
            android:onClick="onDoneClicked"
            android:text="@string/done" />

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onCancelClicked"
            android:text="@string/cancel" />

        <Button
            android:id="@+id/leaveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onLeaveClicked"
            android:text="@string/leave" />

        <Button
            android:id="@+id/finishButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onFinishClicked"
            android:text="@string/finish" />


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.75"
            android:orientation="horizontal"
            android:id="@+id/topHalf" >

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:orientation="horizontal"
            android:id="@+id/bottomHalf" >

            <ImageButton 
                android:id="@+id/polarCapButton1"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top|left"
                android:background="@drawable/polarcap" />

            <ImageButton 
                android:id="@+id/polarCapButton2"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top|right"
                android:background="@drawable/polarcap" />

        </FrameLayout>  


        <TextView
            android:id="@+id/scores"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageButton 
            android:id="@+id/spaceRockButton1"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|left"
            android:background="@drawable/spacerock" />

        <ImageButton 
            android:id="@+id/spaceRockButton2"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:background="@drawable/spacerock" />
</LinearLayout>    

任何帮助都会受到赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

我看到一些可能成为潜在问题的事情。

  1. 您的线性布局包含多个小部件,其中只有2个包含layout_weight值。要解决此问题,您需要将两个框架布局包装在另一个LinerLayout容器中,在该容器中指定新线性布局中2个框架布局的分布。

  2. 在新的linerLayout中,您需要指定布局的总重量,例如:android:weightSum =“1”。

  3. 根据您的线性布局方向,(如果未指定,则为水平)如果LinearLayout方向为水平,请指定:android:layout_width =“0dp”。如果方向是垂直的,请为子元素指定:android:layout_height =“0dp”。

  4. 此外,在我看来,您希望按钮水平对齐(水平方向),两个框架在按钮下对齐,但在正下方(垂直方向)。

    所以在这种情况下,代码看起来像这样:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/top_layout" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/game_layout" >
    
            <Button
                android:id="@+id/cancelButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onCancelClicked"
                android:text="@string/cancel" />
    
            <Button
                android:id="@+id/leaveButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onLeaveClicked"
                android:text="@string/leave" />
    
            <Button
                android:id="@+id/finishButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onFinishClicked"
                android:text="@string/finish" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1"
            android:id="@+id/ll_layout_frames" >
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.75"
                android:orientation="horizontal"
                android:id="@+id/topHalf" >
            </FrameLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.25"
                android:orientation="horizontal"
                android:id="@+id/bottomHalf" >
    
                <ImageButton 
                    android:id="@+id/polarCapButton1"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top|left"
                    android:background="@drawable/polarcap" />
    
                <ImageButton 
                    android:id="@+id/polarCapButton2"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top|right"
                    android:background="@drawable/polarcap" />
    
              </FrameLayout> 
    
          </LinearLayout> 
    
        <!-- you can insert more widgets here, they will be placed in the vertical linear layout -->
    
    </LinearLayout>