public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="?android:attr/toastFrameBackground"> <TextView android:id="@android:id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_horizontal" android:textAppearance="@style/TextAppearance.Toast" android:textColor="@color/bright_foreground_dark" android:shadowColor="#BB000000" android:shadowRadius="2.75" /> </LinearLayout>
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by 赵晨璞 on 2016/8/11.
*/
public class ToastUtil {
private Toast toast;
private LinearLayout toastView;
/**
* 修改原布局的Toast
*/
public ToastUtil() {
}
/**
* 完全自定义布局Toast
* @param context
* @param view
*/
public ToastUtil(Context context, View view,int duration){
toast=new Toast(context);
toast.setView(view);
toast.setDuration(duration);
}
/**
* 向Toast中添加自定义view
* @param view
* @param postion
* @return
*/
public ToastUtil addView(View view,int postion) {
toastView = (LinearLayout) toast.getView();
toastView.addView(view, postion);
return this;
}
/**
* 设置Toast字体及背景颜色
* @param messageColor
* @param backgroundColor
* @return
*/
public ToastUtil setToastColor(int messageColor, int backgroundColor) {
View view = toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundColor(backgroundColor);
message.setTextColor(messageColor);
}
return this;
}
/**
* 设置Toast字体及背景
* @param messageColor
* @param background
* @return
*/
public ToastUtil setToastBackground(int messageColor, int background) {
View view = toast.getView();
if(view!=null){
TextView message=((TextView) view.findViewById(android.R.id.message));
message.setBackgroundResource(background);
message.setTextColor(messageColor);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 短时间显示Toast
*/
public ToastUtil Short(Context context, int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
}
/**
* 长时间显示Toast
*/
public ToastUtil Long(Context context, CharSequence message){
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 长时间显示Toast
*
* @param context
* @param message
*/
public ToastUtil Long(Context context, int message) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 自定义显示Toast时间
*
* @param context
* @param message
* @param duration
*/
public ToastUtil Indefinite(Context context, int message, int duration) {
if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
toast= Toast.makeText(context, message,duration);
toastView=null;
}else{
toast.setText(message);
toast.setDuration(duration);
}
return this;
}
/**
* 显示Toast
* @return
*/
public ToastUtil show (){
toast.show();
return this;
}
/**
* 获取Toast
* @return
*/
public Toast getToast(){
return toast;
}
}
ToastUtil toastUtil=new ToastUtil(); toastUtil.Short(MainActivity.this,"自定义message字体、背景色").setToastColor(Color.WHITE, getResources().getColor(R.color.colorAccent)).show();
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!-- 填充的颜色 --> <solid android:color="#ffc107" /> <!-- android:radius 弧形的半径 --> <corners android:radius="20dip" /> </shape>
toastUtil.Short(MainActivity.this,"自定义message字体颜色和背景").setToastBackground(Color.WHITE,R.drawable.toast_radius).show();
ImageView toastImage = new ImageView(getApplicationContext()); toastImage.setImageResource(R.mipmap.ic_launcher); toastUtil.Short(MainActivity.this,"向Toast添加了一个ImageView").setToastBackground(Color.WHITE,R.drawable.toast_radius).addView(toastImage,0).show();
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.image,null); new ToastUtil(MainActivity.this,view,Toast.LENGTH_SHORT).show();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有