import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.starot.spark.R;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 描述:
* <p>
* 自定义toast
*
* @author allens
* @date 2018/3/26
*/
public class ToastUtil {
private static ViewHolder viewHolder;
public static void show(Context context, String msg) {
Toast toast = new Toast(context);
View layout = View.inflate(context, R.layout.toast, null);
viewHolder = new ViewHolder(layout);
layout.setAlpha(0.7f);
toast.setView(layout);
viewHolder.toastTv.setText(" " + msg + " ");
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.show();
}
public static void show(Context context, int msgRes) {
Toast toast = new Toast(context);
View layout = View.inflate(context, R.layout.toast, null);
viewHolder = new ViewHolder(layout);
layout.setAlpha(0.7f);
toast.setView(layout);
viewHolder.toastTv.setText(" " + context.getResources().getString(msgRes) + " ");
toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
toast.show();
}
static class ViewHolder {
@BindView(R.id.toast_tv)
TextView toastTv;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
<?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="vertical">
<TextView
android:id="@+id/toast_tv"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="center"
android:background="@drawable/btn_bg_toast"
android:gravity="center"
android:text="asdf"
android:textColor="@color/main_adapter_bg" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="4dp" />
<stroke
android:width="0.3dp"
android:color="@color/toast_bg" />
<solid android:color="@color/toast_bg" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>