小编Cor*_*don的帖子

Jetpack 中的权重组合

是否可以在 Jetpack Compose 中称重?例如,我想以这样的方式设置它,即一个项目的权重为布局的 1/3,而另一个则占据剩余的 2/3。

在 XML/ViewGroup 样式中,您可以使用 LinearLayouts 和 ConstraintLayouts 来实现这一点。然而,令我沮丧的是,似乎无法使用 Jetpack Compose。

示例

在 ConstraintLayout 中,这是按如下方式完成的:

<?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>
Run Code Online (Sandbox Code Playgroud)

在 LinearLayouts 中,这是按如下方式完成的:

<?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>
Run Code Online (Sandbox Code Playgroud)

我知道你可以使用 Tables 来获得均匀分布的东西,但我想要一个不均匀的分布。

android kotlin android-jetpack-compose

18
推荐指数
2
解决办法
9711
查看次数

标签 统计

android ×1

android-jetpack-compose ×1

kotlin ×1