布置视图

Laying out the views

我试过但还没有找到解决方案。
我需要 Layout 位于其父项的底部。 这是一个代码:

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

    <TextView
        android:id="@+id/output"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:text="@string/Q1"
        android:padding="16dp"
        />
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/true_button"
            android:text="@string/true_button_text"
            />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/false_button"
            android:text="@string/false_button_text"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_gravity="bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_left"
        android:contentDescription="Back"
        />
    <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"
        android:contentDescription="Next"
        />
    </LinearLayout>

</LinearLayout>

我需要最后两个 ImageButton 在底部。
属性 android:layout_gravity="bottom" 没有帮助
有什么建议吗?

试试这个布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="QI" />

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

            <Button
                android:id="@+id/true_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/false_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/prev_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Back"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Next"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</RelativeLayout>

希望这能解决您的问题。

您是否尝试过使用 relativeLayout 作为父布局并在包含按钮的线性布局中设置 layout_gravity 属性?

在这种情况下使用 RelativeLayout 是最好的方法,但如果您不想切换到 RelativeLayout,而是想坚持使用 LinearLayout,您可以分配 'android:layout_weight="1"'到包含 "imagebuttons".
的线性布局正上方的线性布局 注意:虽然通过这种方式您将能够按下底部的图像按钮,但这会用其上方的布局填充屏幕的其余部分(在这种情况下,包含 "buttons").
注意:这种方法在想要将布局(包含按钮或任何东西)放置在包含列表的屏幕底部的情况下非常有用。

编辑: 令我吃惊的是,您甚至可以在包含图像按钮的布局之上放置一个空的线性布局,并设置属性 'android:layout_weight="1"' 在这个新添加的布局中。这样即使按钮也不会全屏。

也可以尝试使用线性布局 weightweighSum 属性

只需玩 layout_weight 即可满足您的要求

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

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="bottom"
        android:padding="16dp"
        android:text="Q1" />

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

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="true_button_text" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="false_button_text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="3"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/prev_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Back"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Next"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

希望对你有用......