如何在Android Studio-XML中证明内容的合理性



我想知道是否有可能在Android Studio中证明LinearLayout的内容,就像我通常在web中所做的那样。

Web CSS

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

space-around所做的是在一行中均匀分布,周围有相等的空间,使内容适合div(链接(

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

安卓

<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>

虽然这并不能完全回答你的问题,因为它没有使用LinearLayout,但谷歌已经提供了所谓的flexbox布局的Android实现,其中有很多你可能已经熟悉的CSS设置。

它还支持证明内容的合理性,如果你向下滚动阅读自述,其中包括很多例子。

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

将类似CSS的内容行为应用于XML视图的简单方法是使用ConstraintLayout、分配约束和操纵链样式(app:layout_constraintVertical_chainStyleapp:layout_constraintHorisontal_chainStyle(

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

最新更新