MaterialDesign常用控件记录

2020-06-23  本文已影响0人  Shimmer_

[TOC]

1. Snackbar

为一个操作提供轻量级的、快速的反馈,可以它指定显示的位置、文本内容及可选的操作按钮,能在指定时间结束后消失、也可以配合CoordinatorLayout使用进行滑动删除

private void showSnackbar() {
    Snackbar.make(llContent,"这是一条Snackbar!",Snackbar.LENGTH_SHORT)
            .setAction("点击事件", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Toast.makeText(MainActivity.this, "点击Snackbar", Toast.LENGTH_SHORT).show();
                 }
             })
             .setDuration(Snackbar.LENGTH_SHORT).show();
    }

2. TextInputLayout

为输入框的提示提供一个平滑的过渡动画,同时对输入内容校验结果提供一个友好的提示

//配置校验提示
private void setTIL() {
    til.getEditText().addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length()<6) {
                til.setErrorEnabled(true);
                til.setError("长度过短!");
            }else {
                til.setErrorEnabled(false);
            }
        }
    });
}
//XML配置使用
 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/til"
            style="@style/text_style"
            app:errorTextColor="#ffffff"
            app:hintTextColor="#ff0000"
            android:textColorHint="#ffffff">
            <EditText
                android:layout_width="match_parent"
                android:background="@null"
                android:hint="2.TextInputlayout"
                android:textColor="#ffffff"
                android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>

3. DrawerLayout、TabLayout、Navigation——通用主页

  1. DrawerLayout 作为根布局使用,子布局1作为主屏幕内容,子View2作为菜单内容
  2. 子View2需要指定android:layout_gravity 属性 left,right,start(根据系统语言方向决定)来决定滑出的方向

4. CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout、Toolbar、NestedScrollView、FloatingActionButton——协调布局及配合使用的控件

上一篇 下一篇

猜你喜欢

热点阅读