相关疑难解决方法(0)

如何使ConstraintLayout中的元素居中

ConstraintLayout在我的应用程序中使用来进行应用程序布局.我正在尝试创建一个屏幕,其中一个EditText并且Button应该位于中心,并且Button应该低于EditTextmarginTop仅16dp.

这是我的布局和屏幕截图,它现在看起来如何.

activity_authenticate_content.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="com.icici.iciciappathon.login.AuthenticationActivity">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/client_id_input_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/login_client_id"
            android:inputType="textEmailAddress" />

    </android.support.design.widget.TextInputLayout>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/authenticate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/login_auth"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@id/client_id_input_layout"
        app:layout_constraintRight_toRightOf="@id/client_id_input_layout"
        app:layout_constraintTop_toTopOf="@id/client_id_input_layout" />

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

在此输入图像描述

android android-layout android-constraintlayout

176
推荐指数
7
解决办法
14万
查看次数

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

我正在使用新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万
查看次数