Jetpack 中的权重组合 [英] Weights in Jetpack compose
问题描述
是否可以在 Jetpack Compose 中进行称重?例如,我想以这样的方式设置它,即一个项目的权重为布局的 1/3,而另一个则占据剩余的 2/3.
在 XML/ViewGroup 样式中,您可以使用 LinearLayouts 和 ConstraintLayouts 来实现这一点.然而,令我沮丧的是,似乎无法使用 Jetpack Compose.
示例:
在 ConstraintLayout 中,这是按如下方式完成的:
<查看android:layout_width="0dp"android:layout_height="100dp"android:id="@+id/红色"机器人:背景=@颜色/红色"应用程序:layout_constraintStart_toStartOf="parent"应用程序:layout_constraintEnd_toStartOf="@+id/blue"app:layout_constraintHorizontal_weight="1"/><查看android:layout_width="0dp"android:layout_height="100dp"android:id="@+id/蓝色"机器人:背景=@颜色/蓝色"应用程序:layout_constraintStart_toEndOf="@id/red"应用程序:layout_constraintEnd_toEndOf="父"app:layout_constraintHorizontal_weight="2"/></androidx.constraintlayout.widget.ConstraintLayout>
在 LinearLayouts 中,这是按如下方式完成的:
<查看android:layout_width="0dp"android:layout_height="100dp"android:id="@+id/红色"机器人:背景=@颜色/红色"android:layout_weight="1"/><查看android:layout_width="0dp"android:layout_height="100dp"android:id="@+id/蓝色"机器人:背景=@颜色/蓝色"android:layout_weight="2"/></LinearLayout>
我知道你可以使用表格来获得均匀分布的东西,但我想要一个不均匀分布.
使用 1.0.x
你可以使用
Is it possible to do weights in Jetpack Compose? For example, I'd like to set it up in such a way that one item is weighted as 1/3 of a layout, and the other takes up the remaining 2/3.
In the XML/ViewGroup style, you can achieve this using LinearLayouts and ConstraintLayouts. To my dismay, however, it doesn't seem possible using Jetpack Compose.
Example:
In ConstraintLayout, this is done as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:id="@+id/red"
android:background="@color/red"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/blue"
app:layout_constraintHorizontal_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:id="@+id/blue"
android:background="@color/blue"
app:layout_constraintStart_toEndOf="@id/red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="2"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In LinearLayouts, this is done as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:id="@+id/red"
android:background="@color/red"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="100dp"
android:id="@+id/blue"
android:background="@color/blue"
android:layout_weight="2"/>
</LinearLayout>
I know that you can use Tables to get evenly distributed things, but I want an uneven distribution.
With 1.0.x
you can use the Modifier.weight
Something like:
Row() {
Column(
Modifier.weight(1f).background(Blue)){
Text(text = "Weight = 1", color = Color.White)
}
Column(
Modifier.weight(2f).background(Yellow)
) {
Text(text = "Weight = 2")
}
}
这篇关于Jetpack 中的权重组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!