简体   繁体   中英

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

I would like to place buttons horizontally on large screens and vertically on small screens (if they can't fit in screen resolution). Can I do it by using ConstraintLayout without creating two layouts in xml which depend on screen resolution? I don't know how can I setup "constraints" for that behaviour?

现在像这样工作

It is my layout in 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>

You won't be able to do that with just ConstraintLayout . It does not provide you with a way to create sort of a linebreak for Views in a chain.

There are multiple libraries that can easily let to do that. But you will have to introduce another layout. Eg android-flowlayout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM