3

我有一个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>

在编辑器中查看

运行时查看

4

2 回答 2

1

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

于 2016-07-03T01:29:35.540 回答
1

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

app:layout_constraintLeft_toLeftOf="@+id/relativeLayout"

你应该使用:

app:layout_constraintLeft_toLeftOf="parent"

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

于 2016-08-12T18:06:40.920 回答