SnackBar

简介

xxx

使用

原生

final Snackbar snackbar = Snackbar.make(findViewById(R.id.rlDataEntryModules), "进入新增模式", Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.know, new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        snackbar.dismiss();
    }
}).show();

封装

BaseActivity

/**
 * SnackBar
 *
 * @param view        视图
 * @param message     内容
 * @param duration    时长
 * @param sbAlpha     背景透明度
 * @param sbColor     背景色
 * @param textColor   文本色
 * @param textGravity 文本位
 * @return snackBar
 */
public static Snackbar baseSnackBar(View view, String message, int duration, Float sbAlpha, int sbColor, int textColor, int textGravity) {
    Snackbar baseSnackBar = Snackbar.make(view, message, duration);
    View snackBarView = baseSnackBar.getView();
    TextView textView = snackBarView.findViewById(R.id.snackbar_text);
    // SnackBar
    snackBarView.setAlpha(sbAlpha);
    snackBarView.setBackgroundColor(sbColor);
    // TextView
    extView.setTextColor(textColor);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // View.setTextAlignment需SDK>=17
        textView.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
        textView.setGravity(textGravity);
    }
    return baseSnackBar;
}

主代码

baseSnackBar(findViewById(R.id.rlDataEntryModules), "测试", Snackbar.LENGTH_SHORT, 1.0F,
        ContextCompat.getColor(this, R.color.colorPrimary),
        ContextCompat.getColor(this, R.color.background), Gravity.CENTER).show();

猜你喜欢

转载自blog.csdn.net/zsp_android_com/article/details/81031209