在LinearLayout中自动计算边距

问题描述 投票:0回答:2

我有问题。我要实现这一点:enter image description here

我正在使用LinearLayout。 LinearLayout和ChildViews的大小为const。问题是,我的工作结果看起来像这样:enter image description here

这是我的代码:

<LinearLayout
    android:id="@+id/answersLayout"
    android:weightSum="3"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:orientation="horizontal"
    android:background="@drawable/bck_number_red"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/boxOfRecycleViewer" >

    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>

    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>


    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>

</LinearLayout>
android android-layout android-linearlayout
2个回答
0
投票

您可以使用ConstrainLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/firstContainer"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"
        app:layout_constraintEnd_toStartOf="@+id/secondContainer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread_inside" />

    <FrameLayout
        android:id="@+id/secondContainer"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"
        app:layout_constraintEnd_toStartOf="@id/thirdContainer"
        app:layout_constraintStart_toEndOf="@+id/firstContainer"
        app:layout_constraintTop_toTopOf="parent" />


    <FrameLayout
        android:id="@+id/thirdContainer"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/secondContainer"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

0
投票

似乎您正在使用ConstraintLayout来包含LinearLayout。使用ConstraintLayout应该很容易做到这一点。

如果使用LinearLayout可以提供背景,则可以在视图之间使用权重的Space

<Space
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1" />
© www.soinside.com 2019 - 2024. All rights reserved.