如果按钮不适合屏幕,请将一个按钮放在 ConstraintLayout 中另一个按钮的下方

Place one button below another in ConstraintLayout if they can't fit on screen

我想在大屏幕上水平放置按钮,在小屏幕上垂直放置按钮(如果它们不适合屏幕分辨率)。我可以通过使用 ConstraintLayout 来做到这一点,而无需在 xml 中创建两个取决于屏幕分辨率的布局吗? 我不知道如何为该行为设置 "constraints"?

这是我在xml

中的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
       android:id="@+id/button5"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="First button"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toLeftOf="@+id/button3" />

    <Button
       android:id="@+id/button3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Second button"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toRightOf="@+id/button5"
       app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

ConstraintLayout 是做不到的。它没有为您提供在链中为 Views 创建某种 linebreak 的方法。

有多个库可以轻松做到这一点。但是您将不得不引入另一种布局。例如。 android-flowlayout.