【问题标题】:How to justify the content in Android Studio - XML如何证明 Android Studio 中的内容 - XML
【发布时间】:2020-03-12 11:09:24
【问题描述】:

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

网页 CSS

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

space-around 所做的是在行中均匀分布,周围等间距使内容适合 div (link)

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

安卓

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

【问题讨论】:

    标签: android xml justify


    【解决方案1】:

    你可以这样: 只需赋予 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>
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2021-09-02
          • 1970-01-01
          • 2021-10-19
          • 2012-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-28
          相关资源
          最近更新 更多