zoukankan      html  css  js  c++  java
  • 高级UI-Snackbar

    在与用户的交互中,最为常用的Toast和Dialog,但二者都存在其局限,Toast无法与用户进行交互,Dialog虽然可以与用户交互,但却会阻断用户操作的连贯性,介于二者之间的平衡,Snackbar孕育而生

    自定义Toast

    首先我们来做一个自定义的Toast

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void showToast(View v) {
            //Toast.makeText(this,"toast show",Toast.LENGTH_SHORT).show();
            Toast result = new Toast(this);
            LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflate.inflate(R.layout.toast_layout, null);
            result.setView(view);
            result.setDuration(Toast.LENGTH_SHORT);
            result.show();
        }
    }
    

    toast_layout.xml就是一个简单的图片加文字的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher_round" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="自定义Toast" />
    
    </LinearLayout>
    

    Snackbar使用

    public void showSnackbar(View view) {
        //Snackbar.LENGTH_SHORT:短时间
        //Snackbar.LENGTH_LONG:长时间
        //Snackbar.LENGTH_INDEFINITE:不消失
        Snackbar snackbar = Snackbar.make(view,"是否关闭WIFI",Snackbar.LENGTH_INDEFINITE);
        //只能设置单个action
        snackbar.setAction("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(v);
            }
        });
        snackbar.show();
    }
    

    Snackbar的使用其实很简单,设置显示内容,设置action,然后监听就完事了
    其主要优势在于不会打断用户操作,保证用户的使用体验
    项目效果如下:
    Snackbar-实现

  • 相关阅读:
    Java服务停止与开启
    跨域,php后端处理
    Mac 504 gateway timeout nginx
    Tp3.2 多数据库链接
    Redis 设置密码
    Redis 如何对外访问 lnmp安装
    tensorflow gpu安装
    ngx_http_upstream_check_module
    Nginx负载均衡+监控状态检测
    springboot+log4j2+slf4j控制台打印带sql日志
  • 原文地址:https://www.cnblogs.com/cj5785/p/10664598.html
Copyright © 2011-2022 走看看