小编Cla*_*ira的帖子

约束布局按钮文本中心对齐

我正在使用新Constraint布局来构建我的布局.我需要Button占用几乎整个屏幕宽度,这很容易使用约束.

在此输入图像描述

正如您在图像中看到的那样,我将宽度设置为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>
Run Code Online (Sandbox Code Playgroud)

编辑这是ConstraintLayoutalpha4 中的一个错误.

更新谷歌发布了alpha5,上面的代码现在可以使用了. 发行公告

android android-constraintlayout

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

android-constraintlayout ×1