gpt4 book ai didi

android - Toast 背景更改以匹配 Activity 的主题

转载 作者:IT老高 更新时间:2023-10-28 23:01:46 24 4
gpt4 key购买 nike

我为我的 Activity 创建了一个他们都使用的自定义主题。在主题中,我设置了 android:background,这恰好导致任何对话框或 toast 消息看起来很奇怪。

如何防止 toast 和其他对话框吸收主题的属性?

最佳答案

您可以通过以下代码轻松创建自定义 toast :

Toast toast = Toast.makeText(context, resTxtId, Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.custom_bkg);
TextView text = (TextView) view.findViewById(android.R.id.message);
/*here you can do anything with text*/
toast.show();

或者您可以实例化一个完全自定义的 toast:

Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_layout, null);
toast.setView(view);
toast.show();

对话框定制是一个更复杂的例程。但也有类似的解决方法。

关于android - Toast 背景更改以匹配 Activity 的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7020286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com