自定义TextInputLayout android [复制]

问题描述 投票:12回答:2

这个问题在这里已有答案:

我正在尝试创建自定义qazxsw poi。如何创建以下自定义qazxsw poi? qazxsw poi

android android-layout android-textinputlayout
2个回答
12
投票

您应该为Outline Box使用Material Design样式。只需简单使用:

TextInputLayout

TextInputLayout。请参阅enter image description here中的Android文本字段

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"


16
投票

这是一个解决方法:

1.设计你的TextInputLayout结构如下:

activity_test.xml

Material Design Guide

2.使用下面的可绘制enter image description here圆角。

RES /抽拉/ bg_rounded_input_field.xml

layout

3.通过添加属性<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:padding="16dp" android:background="@android:color/white"> <!-- Username --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="match_parent" android:layout_height="52dp" android:layout_marginTop="10dp" android:background="@drawable/bg_rounded_input_field" /> <TextView android:id="@+id/text_dummy_hint_username" android:layout_width="wrap_content" android:layout_height="2dp" android:layout_marginTop="10dp" android:layout_marginLeft="16dp" android:paddingLeft="4dp" android:paddingRight="4dp" android:text="Username" android:textSize="16sp" android:background="@android:color/white" android:visibility="invisible"/> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:hint="Username" android:textColorHint="@android:color/black" app:hintTextAppearance="@style/HintTextStyle"> <EditText android:id="@+id/edit_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text|textCapWords" android:maxLines="1" android:backgroundTint="@android:color/transparent"/> </android.support.design.widget.TextInputLayout> </RelativeLayout> <!-- Password --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp"> <View android:layout_width="match_parent" android:layout_height="52dp" android:layout_marginTop="10dp" android:background="@drawable/bg_rounded_input_field" /> <TextView android:id="@+id/text_dummy_hint_password" android:layout_width="wrap_content" android:layout_height="2dp" android:layout_marginTop="10dp" android:layout_marginLeft="16dp" android:paddingLeft="4dp" android:paddingRight="4dp" android:text="Password" android:textSize="16sp" android:background="@android:color/white" android:visibility="invisible"/> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:hint="Password" android:textColorHint="@android:color/black" app:hintTextAppearance="@style/HintTextStyle"> <EditText android:id="@+id/edit_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:maxLines="1" android:backgroundTint="@android:color/transparent"/> </android.support.design.widget.TextInputLayout> </RelativeLayout> </LinearLayout> bg_rounded_input_field.xml下面使用<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:color="@android:color/black" android:width="2dp"> </stroke> <corners android:radius="8dp"> </corners> </shape>

RES /值/ styles.xml

HintTextStyle

4.最后,在你的TextInputLayout只是app:hintTextAppearance="@style/HintTextStyle" TextView <style name="HintTextStyle" parent="TextAppearance.Design.Hint"> <item name="android:textSize">16sp</item> </style> Activityshow/hide改变。

仅供参考,我使用text_dummy_hint_username延迟text_dummy_hint_password来显示虚拟提示focusHandler暗示文本100 millis同步。

test activity.Java

TextView

OUTPUT:

TextInputLayout

希望这会有所帮助〜

© www.soinside.com 2019 - 2024. All rights reserved.