繁体   English   中英

如果屏幕上无法容纳一个按钮,请在ConstraintLayout中将其放置在另一个按钮下面

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

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

现在像这样工作

这是我在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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