首页 > 解决方案 > 如何在 Android 上将一些视图设置为 TextInputLayout

问题描述

在我的应用程序中,我想将视图显示到TextInputLayout.
我想要 show EditText, TextView(for show countDownTimer) 和View(for show line)!

如下图:

我的用户界面图片

我写了下面的代码,但我只能显示 EditText 而不能显示任何其他视图,例如 TextView 和 View!

我的 XML 代码:

<android.support.design.widget.TextInputLayout
    android:id="@+id/signInFrag_phoneInpLay"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="8dp"
    app:boxStrokeColor="@color/colorAccent"
    app:boxStrokeWidth="1dp">

    <EditText
        android:id="@+id/edtLogin1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:drawableEnd="@drawable/ic_phone"
        android:drawableRight="@drawable/ic_phone"
        android:gravity="right|center_vertical"
        android:hint="Phone Number"
        android:inputType="phone"
        android:maxLength="11"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:singleLine="true"
        android:textColorHint="#c0c0c0"
        android:textSize="12sp" />

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

如何更改我的显示代码,如上图?

标签: androidxmlandroid-layout

解决方案


textInputLayout您可以通过约束布局将视图设置为重叠。可能有将editText的文本与计数文本或垂直视图重叠的风险。但是我在您的文本视图中发现有文本限制。所以出于你的原因,不可能重叠计数文本视图

代码**

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search Page"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/input_layout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:boxStrokeColor="@color/colorAccent"
        app:boxStrokeWidth="1dp"
        app:hintEnabled="true"
        app:layout_constraintTop_toBottomOf="@+id/title">

        <EditText
            android:id="@+id/edtLogin1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:drawableStart="@drawable/ic_action_profile"
            android:gravity="left|center_vertical"
            android:hint="Phone Number"
            android:inputType="phone"
            android:maxLength="11"
            android:maxLines="1"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:singleLine="true"
            android:textColorHint="#c0c0c0"
            android:textSize="12sp" />
    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        android:id="@+id/text_view_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="22sec"
        app:layout_constraintBottom_toBottomOf="@+id/input_layout"
        app:layout_constraintEnd_toEndOf="@+id/input_layout"
        app:layout_constraintTop_toTopOf="@+id/input_layout" />

    <View
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="0dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/input_layout"
        app:layout_constraintEnd_toStartOf="@+id/text_view_time"
        app:layout_constraintTop_toTopOf="@+id/input_layout" />


</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读