现在的位置: 首页 > 综合 > 正文

自定义toast的风格。

2018年04月04日 ⁄ 综合 ⁄ 共 1220字 ⁄ 字号 评论关闭

public class MyToast {
    
    public static void myToast(Context context, String toast_text) {

        if (null == context || toast_text == null){
            return;
        }
        
        int strokeWidth = 6;
        int roundRadius = 8;
        int strokeColor = Color.parseColor("#a1afc9");
        int fillColor = Color.parseColor("#e0000000");

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(fillColor);
        gd.setCornerRadius(roundRadius);
        gd.setStroke(strokeWidth, strokeColor);

        TextView txtViewToast = new TextView(context);
        txtViewToast.setGravity(Gravity.CENTER);
        txtViewToast.setTextColor(Color.parseColor("#ffffffff"));
        txtViewToast.setTextSize(32);
        txtViewToast.setMaxEms(12);
        txtViewToast.setText(toast_text);
        
        RelativeLayout mRelativeLayout = new RelativeLayout(context);
        mRelativeLayout.setLayoutParams(new LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        mRelativeLayout.setPadding(25, 25, 25, 25);
        mRelativeLayout.setBackground(gd);
        mRelativeLayout.addView(txtViewToast);

        Toast toast = new Toast(context);
        toast.setView(mRelativeLayout);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}

抱歉!评论已关闭.