第一种:
TextInputLayout和EditText配套使用,实现hint上浮效果
有两种写法:
1. 给TextInputLayout设置hint
代码:
<android.support.design.widget.TextInputLayout android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout>
代码:
<android.support.design.widget.TextInputLayout android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码"/> </android.support.design.widget.TextInputLayout>
第二种: TextInputLayout 输入框检查提示,会在输入框的下面显示错误提示信息
这里先提供一个参考的博客:
这里直接上代码:
textInputLayout.getEditText().addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if(charSequence.length() < 6){ textInputLayout.setError("用户名不能少于6位!"); }else{ textInputLayout.setErrorEnabled(false); } } @Override public void afterTextChanged(Editable editable) { } });
这个是基于第一种效果的,也就是TextInputLayout和EditText必须是一对一的,而且TextInputLayout只能包一个EditText(而非其他控件),可以看到这里的TextInputLayout 用一个getEditText()获取它里面包含的EditText,提示相关的方法有:
1). TextInputLayout.setErrorEnable(boolean) ,true表示显示提示信息,false不显示。
2). TextInputLayout.setError(string)设置提示信息,方法里面调用了setErrorEnable(true),所以不用在需要的地方专门设置这个属性。
效果图:
第三种:使用CardView实现卡片布局,边缘带阴影 ,使用如下:
gradle:
compile 'com.android.support:cardview-v7:25.1.1'
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="10dp" app:cardElevation="10dp" android:layout_margin="15dp"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:background="@color/white" android:layout_height="wrap_content"> <com.facebook.drawee.view.SimpleDraweeView android:layout_width="80dp" android:layout_height="80dp" android:layout_margin="10dp" android:background="@mipmap/bg"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:layout_marginBottom="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:text="如风而过"/> <TextView android:layout_marginLeft="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="2" android:ellipsize="end" android:textSize="16sp" android:layout_marginRight="10dp" android:text="如风而过如风而过如风而过如风而过如风而过如风而过如风而过如风而过如风而过"/> </LinearLayout> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>上面CardView中的属性也可以用代码,用法和注释如下:
cardView.setRadius(8);//设置图片圆角的半径大小
cardView.setCardElevation(8);//设置阴影部分大小
cardView.setContentPadding(5,5,5,5);//设置图片距离阴影大小
效果如下: