如何在android中绘制3个按钮,它们之间的垂直距离相等?

Ama*_*ena -4 android android-layout

无论屏幕尺寸如何,我都需要在每个按钮之间从上到下保持相等的垂直距离.这是相同的代码:

  • 设计应适用于所有类型的屏幕尺寸.
  • 没有要使用的静态值.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:orientation="vertical"
       android:weightSum="3"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Stu*_*ing 5

一种可能的方法是使用a ConstraintLayout而不是a LinearLayout.这将允许您将这3个按钮链接在一起,这将导致它们等距.

有关如何创建窗口小部件链的更多详细信息,请参阅此文章