首页 > 解决方案 > LinearLayout 未正确居中

问题描述

大家下午好。

我有两个 LinearLayouts 一个在另一个之下。两者都包括两个居中的按钮。

我需要顶部布局和底部布局上的按钮之间的空白区域匹配而不更改宽度按钮。

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layout_contenedorContador">
<LinearLayout
                android:id="@+id/layout_botonRespuesta"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layout_textoPregunta">

                <Button
                    android:id="@+id/button7"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/quiz_falso" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/quiz_verdadero" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout_botonNav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="100"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layout_botonRespuesta">

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

                    android:text="@string/quiz_atras" />

                <Button
                    android:id="@+id/button8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/quiz_siguiente" />
            </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

设计形象

更新

在这里,我展示了一张图像,表明我想要如何分配。红线仅作引导

想象我想要的设计

标签: androidandroid-layout

解决方案


改变

android:weightSum="2" 的线性布局和按钮宽度为 android:layout_width="0dp" 并添加布局权重线 android:layout_weight="1"。

 <LinearLayout
        android:id="@+id/layout_botonRespuesta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layout_textoPregunta">

        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="falso" />

        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="verdadero" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_botonNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layout_botonRespuesta">

        <Button
            android:id="@+id/button10"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="atras" />

        <Button
            android:id="@+id/button8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="siguiente" />
    </LinearLayout>

推荐阅读