如何在style.xml中定义Toast样式?

时间:2016-09-16 16:37:40

标签: android toast

Toast可以在style.xml中设置样式,就像我们为活动主题做的那样吗?

我想设计以下内容:

  • 文字颜色
  • 文字大小
  • 文字字体
  • 背景颜色/不透明度
  • 背景角和尺寸的半径

我无法在网络或style.xml中找到与Toast相关的任何内容

我已经通过制作一个StyleableToast类来解决它,你几乎可以用它来轻松地设置你的Toasts样式!请在此处查看答案: https://stackoverflow.com/a/39591755/5366495

4 个答案:

答案 0 :(得分:3)

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

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

就是这样。 您只需在项目中添加一个课程: https://github.com/Muddz/StyleableToast

使用StyleableToast制作的吐司示例:

enter image description here

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

答案 1 :(得分:1)

我为此创建了自己的类。因为它可以防止我从其他人的解决方案中遇到麻烦 这是它的外观:

Example

您可以在一行中显示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>

答案 2 :(得分:0)

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

echo 'alert("Successful signup your order number is' . $row['number'] . '")';

这是custom_toast.xml:

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();

希望这会对你有所帮助。

答案 3 :(得分:0)

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

您可以使用类似于吐司的方式,但您也可以设置内容应如何显示的布局。

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

我认为这是最好的方式,因为它允许你做你在问题中提出的所有事情。