2016-08-03 50 views
4

我正在使用新的Constraint布局来构建我的布局。我需要占用几乎整个屏幕宽度的Button,并且使用约束很容易。约束布局按钮文本中心对齐

enter image description here

正如你可以在图片中看到我设置宽度0dp(任何大小),但文本不会在中间有什么通常是正常的一个按钮上的文字贴。

我已经试过: - 设置重力中心 - 设置textAlignment居中

貌似这个属性不能用0dp宽度(任意尺寸)工作。

所以我试图用重心设置宽度为match_parent

enter image description here

这是一点点的权利。

有没有人知道如何解决这个问题?

请注意,我用的α4

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的α4的错误。

UPDATE Google已发布alpha5,上述代码现在可用。 Release note

回答

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> 

受Hobo joe的解决方案的启发,下面是我喜欢做的方式。他的解决方案正在工作,但仍需要使用填充来创建间距。如果使用保证金,则按钮文本的对齐将稍微偏向右侧。所以我在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> 
+1

谷歌已经发布了alpha5,更新它,你不会再需要这项工作了。 –

+0

谢谢@ClaytonOliveira。我可以更新我的答案吗? –

0

这是一个错误。但是,您可以通过将按钮放在LinearLayout(或其他标准ViewGroup)中来解决该问题。将父视图和按钮宽度设置为match_parent,并将按钮上的所有约束移至父布局。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_constraintLeft_toLeftOf="@+id/parent_left" 
    app:layout_constraintTop_toTopOf="@+id/parent_top" 
    app:layout_constraintRight_toRightOf="@+id/parent_right"> 

    <Button 
     android:id="@+id/test" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Centered Text"/> 

</LinearLayout> 
+0

我做了你的建议,但没有奏效。 –

+0

你可以发布你的布局代码吗?我刚刚用我发布的XML进行了测试,结果很有用。 –

+0

添加视图代码! –

0

嗯,我想它的,因为这些限制 应用:layout_constraintRight_toRightOf 应用:layout_constraintLeft_toLeftOf

与此一替换当前的按钮:

<Button 
    android:text="Log in" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:id="@+id/btLogin" 
    android:textColor="@android:color/white" 
    android:background="@color/BackgroundColor" 
    android:gravity="center" 
    android:textAlignment="center" 
    android:layout_marginTop="100dp" 
    tools:layout_editor_absoluteX="-1dp" 
    app:layout_constraintTop_toBottomOf="@+id/textView6" /> 

希望这会有所帮助。

3

你试过了吗?

android:textAlignment="center" 

这对我有用。