【问题标题】:ConstraintLayout ignoring constraintsConstraintLayout 忽略约束
【发布时间】:2016-11-04 04:40:16
【问题描述】:

我有一个ConstraintLayout,有几个元素和两个按钮。

我试图通过使用顶部和底部约束并使用偏差属性将按钮放在底部以获取底部/左/右对齐。

一切都在编辑器中正确显示 (Android studio 2.2 P3),但是当我运行应用程序时,没有任何内容正确显示。

任何帮助都将不胜感激。

这是layout XML的相关部分

<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"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout"
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

   ...snip...

    <Button
        android:text="Reset"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/relativeLayout"
        app:layout_constraintBottom_toBottomOf="@+id/relativeLayout"
        android:layout_marginBottom="16dp" />

    <Button
        android:text="Apply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profile_apply_button"
        app:layout_constraintLeft_toLeftOf="@+id/relativeLayout"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintTop_toTopOf="@+id/relativeLayout"
        android:layout_marginTop="16dp"
        app:layout_constraintRight_toRightOf="@+id/relativeLayout"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/relativeLayout"
        android:layout_marginBottom="16dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintVertical_bias="1.0" />
 </android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    我能够通过使用设置为左/下/右边缘的垂直和水平指南来解决这个问题,并且我的控件正确地与这些指南对齐。

    【讨论】:

      【解决方案2】:

      我猜这是从片段加载的?如果是这样,而不是使用类似的东西:

      app:layout_constraintLeft_toLeftOf="@+id/relativeLayout"
      

      你应该使用:

      app:layout_constraintLeft_toLeftOf="parent"
      

      (自 ConstraintLayout alpha 5 起可用)。最好也更新到最新版本的 Android Studio(例如2.2 beta),编辑器会为您完成替换这些引用的工作。

      【讨论】:

        猜你喜欢
        • 2015-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 2010-10-17
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多