【问题标题】:All items merges to top left in constraint Layout , Android Studio [duplicate]所有项目在约束布局中合并到左上角,Android Studio [重复]
【发布时间】:2017-04-15 06:16:09
【问题描述】:

我是初学者,这是我运行应用程序时的样子。我正在使用最新版本的 Android Studio。默认约束布局是截图

这就是它的显示

https://drive.google.com/file/d/0B-ydaOiOKnYnOG1oODVoZEtwRzA/view?usp=drivesdk

这就是我想要设计的

https://drive.google.com/file/d/0B-ydaOiOKnYnQ2NKSXdZencxYk0/view?usp=drivesdk

帮助将是可观的。

【问题讨论】:

  • 请包含包含您已添加到元素的约束的 XML。如果您没有添加任何约束,这就是您的元素都在左上角的原因。添加约束以正确放置它们。

标签: android android-layout android-activity android-constraintlayout


【解决方案1】:

这是您想要使用 ConstraintLayout 实现的目标

<android.support.constraint.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="match_parent">

    <TextView
        android:id="@+id/email_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EMAIL"
        android:textSize="22sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <EditText
        android:id="@+id/email_input"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="16dp"
        android:background="@android:color/white"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email_label" />

    <TextView
        android:id="@+id/pwd_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="PWD"
        android:textSize="22sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email_input" />

    <EditText
        android:id="@+id/pwd_input"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_margin="16dp"
        android:background="@android:color/white"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pwd_label" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOGIN"
        android:textSize="16sp"
        android:layout_margin="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pwd_input" />

</android.support.constraint.ConstraintLayout>

在电子邮件标签的小部件中,您还可以删除“layout_constraintVertical_bias”和“layout_constraintBottom_toBottomOf”属性并修复上边距。
这也可以使用链来完成。
确保您使用的是 ConstraintLayout 1.0.2。

您可以阅读这篇关于 ConstraintLayout 的精彩教程
Building interfaces with ConstraintLayout

希望这会有所帮助!

【讨论】:

  • 谢谢拉米,它帮了我很多。
猜你喜欢
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2020-03-22
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多