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

Cla*_*ira 5 android android-constraintlayout

我正在使用新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,上面的代码现在可以使用了. 发行公告

Mar*_*uel 5

它有点偏右.

我认为保证金正在引起这些.根据我的经验,它不仅影响按钮.保证金也在拧紧TextInputEditText.

下面是一个工作代码,但请注意android:layout_width="match_parent"按钮.无论何时我在编辑器中单击它,它都会变为android:layout_width="0dp",并破坏按钮对齐.

<?xml version="1.0" encoding="utf-8"?>
<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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button_survey"
        android:layout_width="match_parent" 
        android:layout_height="52dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/activity_main"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main"
        app:layout_constraintRight_toRightOf="@+id/activity_main"
        app:layout_constraintTop_toTopOf="@+id/activity_main"
        tools:text="@string/main_activity_btn_survey"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp" />


</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

受Hobo joe解决方案的启发,下面是我喜欢的方式.他的解决方案正在解决,但仍需要使用填充来创建间距.如果使用了margin,则按钮文本的对齐方式将略微向右移动.所以我在LinearLayout(或ConstraintLayout)上使用填充而不是按钮上的边距.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main"
        app:layout_constraintTop_toTopOf="@+id/activity_main"
        app:layout_constraintRight_toRightOf="@+id/activity_main"
        app:layout_constraintBottom_toBottomOf="@+id/activity_main"
        android:padding="16dp">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:id="@+id/button_survey"
            android:layout_weight="1"
            tools:text="@string/main_activity_btn_survey"
            />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

  • Google已发布alpha5,对其进行了更新,您将不再需要这项工作。 (2认同)