Material Design控件相关使用

本文介绍了Material Design中TextInputLayout与EditText配合实现hint上浮效果的两种方法,并详细讲解了如何设置错误提示信息。此外,还展示了如何使用CardView创建带阴影的卡片布局,包括设置圆角、阴影和内容内边距。
摘要由CSDN通过智能技术生成

第一种:

TextInputLayoutEditText配套使用,实现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>


2.给EditText设置hint

代码:

<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) {

    }
});

这个是基于第一种效果的,也就是TextInputLayoutEditText必须是一对一的,而且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);//设置图片距离阴影大小

效果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值