Toast自定义样式需要在shape文件下设置:
如何创建shape文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#F2292929"/>
<corners
android:radius="6dp"/>
</shape>
solid:指定填充内部颜色
corners:设置圆角
其他详细标签请查阅:shape标签详解
设置Toast内部布局,这里只设置了一个TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:background="@drawable/my_toast"
android:textColor="#FFFFFF"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:paddingLeft="60dp"
android:paddingRight="60dp"
/>
</LinearLayout>
其中,将background的值设置为自定义Toast(shape文件)的drawable。
为自定义toast新建一个类,新建一个showToast方法:
(不会重复显示相同内容的Toast)
【规避多次连续点击相同按钮时,重复显示Toast的问题】
package com.example.suyingxin.mytoast;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyToast {
private static String oldMsg;
protected static Toast toast=null;
private static long oneTime=0;
private static long twoTime=0;
public static void showToast(Context context,String message){
View view =LayoutInflater.from(context).inflate(R.layout.toast_set,null);
TextView showText=view.findViewById(R.id.tv_toast);
if (toast==null){
toast=new Toast(context); //创建toast实例
toast.setView(view);//设置布局
showText.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);//设置toast的显示时间
toast.setGravity(Gravity.BOTTOM,0,229); //设置toast的显示位置,单位是px
toast.show();
Log.d("MyToast----","create new toast");
oneTime=System.currentTimeMillis();//记录系统时间
}else {
twoTime=System.currentTimeMillis();
if (message.equals(oldMsg)){
if (twoTime-oneTime>Toast.LENGTH_SHORT){
showText.setText(oldMsg);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM,0,229);
toast.show();
Log.d("MyToast----","twoTime large than oneTime");
}
Log.d("MyToast----","twoTime is not enough large than oneTime");
}else {
oldMsg=message;
toast.setView(view);
showText.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM,0,229);
toast.show();
Log.d("MyToast----","newMsg not equals oldMsg");
}
}
oneTime=twoTime;
}
}
MainActivity:
package com.example.suyingxin.mytoast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button bt_one;
Button bt_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_one= (Button) findViewById(R.id.bt_one);
bt_two= (Button) findViewById(R.id.bt_two);
bt_one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyToast.showToast(MainActivity.this,"你已点击了按钮");
}
});
bt_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyToast.showToast(MainActivity.this,"你已点击了第二个按钮");
}
});
}
}
效果图:(先按按钮2,然后连续按按钮1)
以上。