简体   繁体   English

Android - GUI布局

[英]Android - GUI layout

I'm new to Android so not very good with GUi layout yet. 我是Android新手,所以GUi布局还不是很好。 I'm building a GUI for my app and just can't get it to behave the way I want. 我正在为我的应用程序构建一个GUI,但却无法让它按照我想要的方式运行。 What I need is 4 buttons at the bottom of the screen stacked horizontally. 我需要的是屏幕底部4个水平堆叠的按钮。 Above the buttons I've placed a SurfaceView which I want to fill the rest of the screen. 在按钮上方,我放置了一个SurfaceView,我想填充屏幕的其余部分。 The result should be something like this (I hope this is clear): 结果应该是这样的(我希望这很清楚):

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

The closeset I got was this: 我得到的密切是这样的:

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

Which unfortunately results in something like this: 不幸的是,结果是这样的:

-----------------
- SurfaceView   -
-----------------
 --- --- --- ---
 -B- -B- -B- -B-
 --- --- --- ---
<?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>

try this. 尝试这个。

layout_weight is a tag which works on the children of linear layout. layout_weight是一个适用于线性布局子元素的标记。 the children will share the orientation measurement depending on weight if it is mentioned. 如果提到的话,孩子们将根据体重分享方向测量。 In your case since the linear Layout is vertical, so they will share the height if weights are mentioned. 在您的情况下,因为线性布局是垂直的,所以如果提到权重,它们将共享高度。

default weightSum of linearLayout is 1, so if you give 1 to any of the children, it takes the remaining space. linearLayout的默认weightSum为1,因此如果给任何一个子节点1,则占用剩余空间。 And lets say parent has 2 children and their weights as 0.6 and 0.4, the first child will occupy 60% of the parent height and the rest 40% by second child. 并且假设父母有2个孩子,他们的体重为0.6和0.4,第一个孩子将占父母身高的60%,其余40%由第二个孩子。

尝试使用最顶层布局的相对布局,并使用alignParentBottom作为按钮栏布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM