[英]How to define a Toast style in style.xml?
可以像我们为 Activity 主题那样在style.xml
中设置Toast
的样式吗?
我想设置以下样式:
我在 web 或 style.xml 上找不到与 Toast 相关的任何内容
我已经通过制作一个 StyleableToast class 解决了这个问题,您可以轻松地使用它以几乎任何方式设置 Toast 的样式:请在此处查看答案: https://stackoverflow.com/a/39591755/5366495
由于没有简单易行的方式(布局,充气等)来设置Toast
样式,因此我决定制作一个具有很多样式可能性的完整Styleable Toast
类!
我将继续改进Styleable Toast
类,使其功能丰富,然后将其发布到jCenter()
以便可以将其添加为dependency
就这个。 您只需在项目中放入一个类: https : //github.com/Muddz/StyleableToast
用StyleableToast制作的吐司示例:
欢迎所有反馈和功能要求!
为什么不尝试制作自己的吐司面包:
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:
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并查找SnackBar 。 这是“材料设计”准则显示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();
除了文档之外,这里还有一些链接可以帮助您。
2) SnackBar的例子
我认为这是最好的方法,因为它可以让您完成问题中所要求的一切。
在 Android 11 (API 30) 中弃用了 Toast 样式,或者更具体地说是setView()
)。 Google 不希望每个应用程序都有不同的 toast 样式。
如果你想要自定义 toast,你将必须实现并显示你的自定义视图,它看起来像一个 toast。
Imo,你不应该这样做并使用没有任何自定义样式的默认吐司,就像谷歌的意图一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.