Material_Design,谷歌新推出的一种沉浸式的应用风格,最近一直在看这方面的东西,这篇主要讲TextInputLayout这个控件的使用。
首先这个TextInputLayout是Android support_v7_design包里的一个类。
然后从类的继承关系来看,这个类是继承自Linearlayout。所以说,其实他的属性跟Linearlayout都差不多。
简单介绍之后,我们可以看看怎么在具体的项目中去使用这个类
首先是我写的这个布局的展示,是一个简单的登录界面:
再看布局代码:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.material_design.com.material_designapplication.Login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_ipset"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:src="@mipmap/login_1" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_ipset"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
app:counterEnabled="true"
app:counterMaxLength="5"
app:counterOverflowTextAppearance="@style/MyOverflowText"
android:id="@+id/view">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/et_logname"
android:hint="用户名" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_below="@+id/view"
android:layout_alignParentStart="true"
android:id="@+id/view2"
app:counterEnabled="true"
app:counterMaxLength="8"
app:counterOverflowTextAppearance="@style/MyOverflowText">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/et_password"
android:hint="密码"
android:password="true" />
</android.support.design.widget.TextInputLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户"
android:id="@+id/ck_rember"
android:checked="true"
android:layout_below="@+id/view2"
android:layout_marginTop="8dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登 录"
android:id="@+id/bt_login"
android:background="@drawable/log"
android:layout_below="@+id/ck_rember"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textColor="@android:color/white"
android:textSize="18dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码找回"
android:id="@+id/tv_reset"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="@color/abc_btn_colored_borderless_text_material" />
</RelativeLayout>
可以从布局的XML文件里看出来,TextInputLayout的使用就是把EditText这个控件放到它的下面就可以了,这样,给你的EditText的hint属性赋值,就可以实现Material_design风格的文字输入提示了,还挺炫酷的。 当然,有个比较难搞的地方就是Edittext的下划线的颜色和提示文字的颜色,在TextInputLayout里面默认使用的是 style.xml下面的colorAccent属性,所以只要修改ColorAccent这个属性的颜色就行。
TextInputLayout加上counter_enable这个属性之后,就可以实现输入字数提示信息了,可输入文字的长度是countmaxLength这个属性决定的,同样 注意: 当我们使用 **app:counterMaxLength **这个属性的时候,一定要设置 app:counterOverflowTextAppearance 属性,不然的话程序运行会报错,这个属性是设置当我们输入字符超过限定的个数时候 EditText控件 整体显示的样式,需要在 **style.xml **文件里面定义一个 自己的style,注意我们自定义的 style 的 **parent **是 **TextAppearance.AppCompat.Small **,拿我上面的程序举例:
<style name="MyOverflowText" parent="TextAppearance.AppCompat.Small">
<item name="android:textColor">#f3672b</item>
</style>
这样定义好后再在 app:counterOverflowTextAppearance 里面设置这个style就行。
最后来个小提示,当我们在 Android Studio 中导入 support design 开发包的时候,版本号最好和 v7包 的版本号一致,不然有些时候会出现莫名其妙的错误:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
以上就是在学习TextInputLayout这个控件时学习到的内容了。。可能还有许多细节没有写出来,希望大家试试,效果还是挺不错的。