我正在使用新的Constraint
布局来构建我的布局。我需要占用几乎整个屏幕宽度的Button
,并且使用约束很容易。约束布局按钮文本中心对齐
正如你可以在图片中看到我设置宽度0dp
(任何大小),但文本不会在中间有什么通常是正常的一个按钮上的文字贴。
我已经试过: - 设置重力中心 - 设置textAlignment居中
貌似这个属性不能用0dp
宽度(任意尺寸)工作。
所以我试图用重心设置宽度为match_parent
。
这是一点点的权利。
有没有人知道如何解决这个问题?
请注意,我用的α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
谷歌已经发布了alpha5,更新它,你不会再需要这项工作了。 –
谢谢@ClaytonOliveira。我可以更新我的答案吗? –