在 Android 中的 ConstraintLayout 中使用 LinearLayout 是否可取?

Is it advisable to use LinearLayout inside ConstraintLayout in Android?

我是 Android ConstraintLayout 的新手,也是 Android 的新手。我有个问题。在 ConstraintLayout 中使用 LinearLayout 是否可取?例如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    app:srcCompat="@drawable/landing_screen"
    android:layout_height="match_parent"
    tools:context="com.braigolabs.braigo.landing.LandingActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/landing_screen"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:layout_constraintHorizontal_bias="1.0"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="51dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="66dp"
            android:layout_marginStart="66dp"
            android:gravity="center"
            android:text="@string/login_welcome_braigolabs"                android:textAppearance="@style/TextAppearance.AppCompat.Large"
            tools:layout_editor_absoluteX="93dp"
            tools:layout_editor_absoluteY="403dp"/>

        <Button
            android:id="@+id/login_button"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="2dp"
            android:text="@string/login_login_button_title"
            android:textAllCaps="false"
            tools:layout_editor_absoluteX="116dp"
            tools:layout_editor_absoluteY="543dp"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

也很想知道 ConstraintLayout 在开发者中有多受欢迎?

Is it advisable to use LinearLayout inside ConstraintLayout?

没有...通常。

一般来说,ConstraintLayout 背后的想法是,它允许您定位所有子项,而不必在 ConstraintLayout 中嵌套任何其他 ViewGroup。因此,我会说这不是 可取的

但是, 有一些 LinearLayout 可以做到而 ConstraintLayout 做不到的事情(主要围绕视图的加权间距),因此,如果您在布局中需要这些特殊的极端情况,除了退回到 LinearLayout.

之外别无选择

how popular is the ConstraintLayout among the developers?

ConstraintLayout 相对较新,但它非常强大,您当然应该熟悉它。它并不总是手头工作的完美工具,但它通常可以让您轻松创建布局,否则您可能会花费数小时。

我无法谈论广泛采用的统计数据,但我可以说我已经在这个网站上看到大量关于正确使用 ConstraintLayout 的问题,所以很明显世界各地的开发者都开始使用它。

constraintlayout 库的 2.0.0-alpha5 版本开始,现在可以在 ConstraintLayout 中声明一个 Flow 虚拟布局元素(顾名思义)确定所选项目在 ConstraintLayout 中的流动方式(例如垂直、水平)。因此不再需要在 ConstraintLayout.

中声明 LinearLayout

例如,如果您希望 ConstraintLayout 中的项目垂直流动,您可以这样做:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="I am the first TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="I am the second TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="I am the third TextView" />

    <androidx.constraintlayout.helper.widget.Flow
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:constraint_referenced_ids="textView1,textView2,textView3"
        app:flow_horizontalAlign="start"
        app:flow_verticalGap="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

您可以尝试使用 Flow 元素中的 app:flow_ 属性来实现不同的流行为。有关 Flow 元素的更多信息,请参阅发行说明 here. For an example, see here.