如何证明 Android Studio 中的内容 - XML

How to justify the content in Android Studio - XML

我想知道是否可以像我通常在 Web 上所做的那样在 Android Studio 中证明 LinearLayout 的内容。

网页CSS

.space-around {
  display: flex;
  justify-content: space-around;
}

space-around 所做的是在它们周围均匀分布 space 使内容适合 div (link)

如何在 LinearLayout 的 Android Studio 中获得相同的效果?

Android

  <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:orientation="horizontal">
  </LinearLayout>

你可以这样: 只需给 LinearLayout 的内部元素相同的权重:

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

将类似于 CSS 的内容行为应用于 XML 中的视图的简单方法是使用 ConstraintLayout、分配约束并操纵 chain stylesapp:layout_constraintVertical_chainStyle app:layout_constraintHorisontal_chainStyle)

您也可以使用 LinearLayout 做类似的事情,在某些情况下您需要添加透明 View 并调整它们的权重 android:layout_weight

虽然这没有完全回答你的问题,因为它没有使用 LinearLayout,但是 Google 提供了一个 Android implementation of the so-called flexbox layout,其中有很多你可能已经使用的设置从CSS.

熟悉

它还支持 justify-content,如果您向下滚动阅读自述文件,其中包含很多示例。

当然,要使用它,您必须添加对它的依赖。如果您还没有使用 AndroidX,请使用 1.0.0 版本,否则请使用最新的稳定版本。