Android 如何在style.xml中定义Toast样式?
Android 如何在style.xml中定义Toast样式?,android,toast,Android,Toast,Toast是否可以像对待活动主题那样使用style.xml来设置样式 我想设计以下样式: 文本颜色 文本大小 文本字体 背景色/不透明度 角点的背景半径和尺寸 我在web或style.xml中找不到任何与Toast相关的内容 我已经通过制作StyleableToast类解决了这个问题,您可以轻松地使用该类以几乎任何方式设计您的祝酒词!请参见此处的答案:为什么不尝试制作自己的吐司布局: LayoutInflater inflater = getLayoutInflater(); View cu
Toast
是否可以像对待活动主题那样使用style.xml
来设置样式
我想设计以下样式:
- 文本颜色
- 文本大小
- 文本字体
- 背景色/不透明度
- 角点的背景半径和尺寸
我已经通过制作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();
下面是自定义的_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 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();
除了文档之外,这里还有一些帮助您的链接
(一)
(二)
我认为这是最好的方法,因为它可以让你完成你在问题中提出的所有问题。我为此创建了自己的类。因为它可以防止我从别人的解决方案中遇到麻烦。
这就是它的样子: 您可以在一行中显示简单的祝酒词:
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
类,使其功能更加丰富,并在jCenter()
中发布它,以便将其添加为依赖项
给你。只需在项目中添加一个类:
使用StyleableToast制作的烤面包示例:
欢迎所有反馈和功能请求 我认为这是可能的。也许有帮助。@padeg我知道这个链接。但是,如果可以用style来完成这些任务,那么就没有一个任务了。xml
不是你想要的方式吗?@padeg只有在风格上不可能的情况下才这样做。xml@padeg我创造了一个非常好的解决方案。检查我的回答如果无法使用stye.xml,此解决方案将是最后的选择。考虑到您可以使用此解决方案进行定制,我认为使用此解决方案更舒适:)嘿,又来了!检查我的答案。我已经创建了一个StyleableToast
类,它可以用多种方式制作一个Toast,而且非常简单!又来了!检查我的答案。我已经创建了一个StyleableToast
类,它可以用多种方式制作一个Toast,而且非常简单!我认为你的课堂是一个更好的答案,只要你不想要互动。但有了Snackbar,你也可以像点击按钮一样进行交互。此外,您还可以使用CoordinatorLayout移动SnackBar。我觉得这是一个更好的方法,如果你想让Toast消息做的不仅仅是UI显示。嗨。我只是想为我的应用程序干杯,以显示小的重要消息,而不是动作或按钮。我制作了一个类,它以一种非常简单的方式处理所有这一切,同时也使得用许多不同的样式属性来设置祝酒词的样式变得非常容易。检查我的答案。你好,链接好像断了。项目是否仍处于活动状态?是否有人使用其GitHub页面中提供的安装说明成功地将此库集成到您的项目中?由于某些原因,尽管在Gradle模块:app文件中包含了实现,但androidstudio无法加载它。