【问题标题】:Android ConstraintLayout vertical chain weighted spacesAndroid ConstraintLayout 垂直链加权空间
【发布时间】:2021-09-24 22:34:45
【问题描述】:

这个想法是让垂直链在项目之间具有不同的空间。更清楚地说,这里是 LinearLayout 所需行为的示例。

<?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="300dp"
    android:background="@android:color/darker_gray"
    android:orientation="vertical">

    <View
        android:id="@+id/space1"
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:layout_height="0dp" />

    <View
        android:id="@+id/contentView1"
        android:layout_width="match_parent"
        android:layout_weight="0"
        android:layout_height="70dp"
        android:background="@android:color/black" />

    <View
        android:id="@+id/space2"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

    <View
        android:id="@+id/contentView2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_weight="0"
        android:background="@android:color/black" />

    <View
        android:id="@+id/space3"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />
</LinearLayout>

这里 layout_weight="0" 使内容视图包裹其高度。 layout_weight="1"layout_weight="2" 使视图填充可用空间与其权重成正比。如果内容视图变大,空间就会变小(如果没有空间,就没有空间)。

由于目标 UI 比垂直堆栈更复杂,我想使用 ConstraintLayout 而不是 LinearLayout。但我没有看到设置链空间百分比的方法。

感谢您的建议。

【问题讨论】:

    标签: android android-constraintlayout constraint-layout-chains


    【解决方案1】:

    在我写这个问题时,我发现了可能的解决方法,可以在链中添加额外的视图。但也许你知道更好的解决方案?

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@android:color/darker_gray">
    
        <View
            android:id="@+id/space1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0"
            app:layout_constraintBottom_toTopOf="@id/contentView1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="spread" />
    
        <View
            android:id="@+id/contentView1"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toTopOf="@id/contentView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/space1" />
    
        <View
            android:id="@+id/contentView2"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/contentView1" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多