TextInputEditText视图未按预期呈现

时间:2016-08-08 09:25:56

标签: android android-constraintlayout

我的问题是,TextInputEditText视图不会扩展为占用父级的整个宽度,因为我期望使用android:layout_width="match_parent"。 下面的代码是应用程序的代码段:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/constraintLayout">
    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/textInputLayout1"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"
        android:layout_marginLeft="16dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline3"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp">
        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textInputEditText1"/>
    </android.support.design.widget.TextInputLayout>
    <android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/textInputLayout2"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toRightOf="@+id/textInputLayout1"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="@+id/constraintLayout"
        android:layout_marginRight="16dp"
        app:layout_constraintTop_toTopOf="@+id/textInputLayout1"
        app:layout_constraintBottom_toBottomOf="@+id/textInputLayout1">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textInputEditText2"
            android:layout_weight="1"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_Percent="51"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="216dp"/>

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

将TextInputLayout元素的宽度设置为match_parent

答案 1 :(得分:1)

您的代码完全正常,但您可能正在使用ConstraintLayout的旧版本(例如alpha 4)。尝试更新到最新的alpha 6,这将解决您的问题(由于API更改,您还需要将app:layout_constraintGuide_Percent="51"替换为app:layout_constraintGuide_percent="0.5"