将默认 Toast 样式应用于自定义 Toast

Apply default Toast style to custom Toast

我需要将默认吐司样式应用于我制作的自定义吐司消息。我发了一条消息,显示颠倒了,像这样:

Toast t = new Toast(activity);
TextView text = new TextView(activity);
text.setText(msg);
text.setRotation(180);
t.setView(text);
t.setDuration(length);

它工作正常,但我不确定如何将我的自定义文本设置为与原始文本具有相同的 "look and feel"。一些消息来源建议使用自定义 toast.xml(参见 here)并设置 layout/view,但我想采用 android 的默认设置。谁能指出我正确的方向?

真的很简单,我解决了。

使用 Toast.makeText(activity, message, length) 正常创建 Toast 我只是抓取视图,旋转它并重置它。

Toast toast = Toast.makeText(activity, msg, length);
View view = toast.getView();
view.setRotation(180);
toast.setView(view);

return toast;

试试这个:

    // Call toast.xml file for toast layout

    View toastRoot = inflater.inflate(R.layout.toast, null);
    Toast toast = new Toast(context);

    // Set layout to toast 
    toast.setView(toastRoot);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
            0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();