正如您在图像中看到的,我将宽度设置为 0dp (任意大小),但文本不会居中,这对于按钮文本来说通常是正常的。

我试过: - 将重力设置为中心 - 将 textAlignment 设置为中心

看起来此属性不能与 0dp 宽度(任何大小)一起使用。

所以我尝试使用重心将宽度设置为 match_parent

它有点偏右。

有谁知道如何解决这种行为?

请注意,我正在使用 alpha4

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'

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:id="@+id/content_login"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="br.com.marmotex.ui.LoginActivityFragment"
    tools:showIn="@layout/activity_login">
    <Button
        android:text="Log in"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/btLogin"
        android:layout_marginTop="48dp"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="@+id/content_login"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/content_login"
        android:layout_marginLeft="16dp"
        android:textColor="@android:color/white"
        android:background="@color/BackgroundColor" />
</android.support.constraint.ConstraintLayout>

编辑 这是 ConstraintLayout alpha4 中的一个错误。

更新 谷歌已经发布了 alpha5 ,上面的代码现在可以工作了。 发行公告

原文由 Clayton Oliveira 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 167
2 个回答