TextInputLayout使用简介
通常使用输入框时都会给出提示信息,但是当用户开始输入时,提示信息就会消失,从Android 5.0开始,google提供了一个新的控件来完善EditText的用户体验,先上效果图。
使用TextInputLayout,首先需要在gradle中添加依赖,打开build.gradle(Module:app)
,在dependencies
块中添加如下内容
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
建议使用24以上的版本,否则将导致有些熟悉无法正常使用。
TextInputLayout继承自LinearLayout,他里面只能包含一个子控件,通常是EditText,接下来看看布局文件中应该如何使用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/login_bg"
tools:context="com.example.tom.visitshop.activity.LoginActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/login_LinearLayout_margin_top"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/login_guide"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/et_name_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterMaxLength="16"
app:counterEnabled=