MaterialDesign_Snackbar
Snackbar的提出实际上是界于Toast和Dialog的中间产物。
Toast: 用户无法交互;
Dialog:用户可以交互,但是体验会打折扣,会阻断用户的连贯性操作;
Snackbar既可以做到轻量级的用户提醒效果,又可以有交互的功能(必须是一种非必须的操作)。
MainActivity:
package com.example.dell.myapplication;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.Snackbar.Callback;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showCustomToast(View v){
// Toast.makeText(this, "吐司", 0).show();
Toast result = new Toast(this);
LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflate.inflate(R.layout.custom_toast, null);
TextView tv = (TextView)view.findViewById(R.id.textView1);
tv.setText("自定义吐司在此!");
// result.setGravity(gravity, xOffset, yOffset)
result.setView(view);
result.setDuration(Toast.LENGTH_LONG);
result.show();
}
public void showSnackbar(View v){
//LENGTH_INDEFINITE:无穷
Snackbar snackbar = Snackbar.make(v, "是否开启加速模式?", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("确定", new OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast(null);
}
});
//不能设置多个action,会被覆盖
snackbar.setAction("取消", new OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast(null);
}
});
snackbar.setCallback(new Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
// TODO Auto-generated method stub
showCustomToast(null);
super.onDismissed(snackbar, event);
}
@Override
public void onShown(Snackbar snackbar) {
// TODO Auto-generated method stub
super.onShown(snackbar);
}
});
snackbar.setActionTextColor(Color.RED);
snackbar.show();
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="66dp"
android:onClick="showCustomToast"
android:text="自定义吐司" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_marginLeft="53dp"
android:layout_toRightOf="@+id/button1"
android:onClick="showSnackbar"
android:text="Snackbar" />
</RelativeLayout>
custom_toast:
<?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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是自定义吐司"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
运行结果:
来源:CSDN
作者:qq_36828822
链接:https://blog.csdn.net/qq_36828822/article/details/103482223