【问题标题】:LinearLayout not centering correctlyLinearLayout 未正确居中
【发布时间】:2021-07-30 18:24:45
【问题描述】:

大家下午好。

我有两个 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>

Design image

更新

在这里,我展示了一张图片,说明了我想要的分布方式。红线仅作引导

Image how I want the design

【问题讨论】:

  • 你能分享一下预期布局的虚拟设计吗?
  • 我希望顶部两个按钮之间的空白区域与底部按钮中的空白区域相匹配。我更新了新图像。 @AndroidGeek

标签: android android-layout


【解决方案1】:

改变

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>

【讨论】:

  • 谢谢。但是该代码更改了按钮的宽度。我需要顶部布局和底部布局上的按钮之间的空白区域匹配而不更改宽度按钮。
  • 您可以设置空白按钮的边距。
【解决方案2】:

使用表格布局代替两个线性布局

 <TableLayout
    android:id="@+id/layout_botonNav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/layout_botonRespuesta">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_margin="15dp"
        >

        <Button
            android:layout_marginRight="5dp"
            android:layout_gravity="end"
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="falso" />

        <Button
            android:layout_marginLeft="5dp"
            android:layout_gravity="start"
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="verdadero" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_margin="15dp"
        >

        <Button
            android:layout_gravity="end"
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="atras" />

        <Button
            android:layout_marginLeft="5dp"
            android:layout_gravity="start"
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="siguiente" />
    </TableRow>


</TableLayout>

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2020-10-31
    • 2016-05-25
    • 1970-01-01
    相关资源
    最近更新 更多