style.xml如何定义Toast样式?

How to define a Toast style in style.xml?

可以像 Activity 主题那样在 style.xml 中设置 Toast 的样式吗?

我想设置以下样式:

我在网络上或 style.xml

中找不到与 Toast 相关的任何内容

我已经通过制作一个 StyleableToast class 解决了这个问题,您可以轻松地使用它来以几乎任何方式设置 Toast 的样式!在这里查看答案:

为什么你不尝试制作自己的 toast 布局:

LayoutInflater inflater = getLayoutInflater();
View customToastroot = inflater.inflate(R.layout.custom_toast, null);
TextView msg = (TextView) customToastroot.findViewById(R.id.toastMsg);
msg.setText("Speed up !");
msg.setTypeface(tf);
Toast customtoast = new Toast(getApplicationContext());
customtoast.setView(customToastroot);
customtoast.setDuration(Toast.LENGTH_SHORT);
customtoast.show();

这里是 custom_toast.xml :

 <LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/game_on">

    <TextView
        android:id="@+id/toastMsg"
        android:layout_margin="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dp"
        android:text="Your text"
        android:textSize="16dp"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

希望对您有所帮助。

我认为你应该停止使用 Toast 并查找 SnackBar。这是 Material 显示 Toast 类型消息的设计指南方式中的新标准。

您可以像吐司一样使用它,但您也可以为内容的显示方式设置布局。

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "Welcome to AndroidHive", Snackbar.LENGTH_LONG);

snackbar.show();

不仅如此,您还可以在 SnackBar 中设置自定义交互,例如按钮点击。例如:

Snackbar snackbar = Snackbar
        .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG)
        .setAction("UNDO", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT);
                snackbar1.show();
            }
        });

snackbar.show();

除了文档之外,这里还有一些可以帮助您的链接。

1) Material Design guidelines for SnackBar

2) SnackBar examples

我认为这是最好的方法,因为它可以让你完成你在问题中提出的所有问题。

我为此创建了自己的 class。因为它避免了我从其他人的解决方案中遇到的麻烦。
这是它的样子:

您可以在一行中显示一个简单的 Toast:

MyToast.showShort(context, getString(R.string.verworfen));
MyToast.showLong(context, getString(R.string.verworfen));

//代码

public class MyToast{

    private static Toast currentToast;

    public static void showShort(Context context, String message){
        if(currentToast != null) currentToast.cancel();
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
        TextView text = (TextView) layout.findViewById(R.id.message);

        text.setText(message);

        Toast toast = new Toast(context);

        toast.setDuration(Toast.LENGTH_SHORT);

        toast.setView(layout);
        toast.show();
        currentToast = toast;
    }

    public static void showLong(Context context, String message){
        if(currentToast != null) currentToast.cancel();
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.root));
        TextView text = (TextView) layout.findViewById(R.id.message);

        text.setText(message);

        Toast toast = new Toast(context);

        toast.setDuration(Toast.LENGTH_LONG);

        toast.setView(layout);
        toast.show();
        currentToast = toast;
    }

    public static Toast getCurrentToast(){
        return currentToast;
    }

}

//布局

<LinearLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/custom_toast">

    <TextView
        android:id="@id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"

        android:layout_gravity="center_horizontal"
        android:textColor="@color/white"
        android:textSize="14sp" />

</LinearLayout>

//可绘制

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="@color/primary_dark" >
    </solid>

    <stroke
        android:width="2dp"
        android:color="@color/primary_light" >
    </stroke>

    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>

    <corners
        android:radius="11dp"   >
    </corners>

</shape>

由于没有一种简单且不混乱的方式(布局、膨胀等)来设计 Toast,我决定制作一个完整的 Styleable Toast class很多造型的可能性!

我会不断改进 Styleable Toast class 并使其功能丰富,然后在 jCenter() 中发布它,以便可以将其添加为 dependency

就在这里。 只需一个 class 放入项目中: https://github.com/Muddz/StyleableToast

使用 StyleableToast 制作的吐司示例:

欢迎所有反馈和功能请求!