Toast:"之间的差异"和" getApplicationContext()"?

时间:2015-10-09 19:06:34

标签: android android-context android-toast android-5.1.1-lollipop

我的设备运行Android 5.1.1,我发现如果我使用

Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show();

我明白了:

"round" toast

但如果我使用getApplicationContext()代替this

Toast.makeText(getApplicationContext(), "This is a toast", Toast.LENGTH_SHORT).show();

我明白了:

rectangle toast

两者都是直接从活动中调用的。

为什么会这样?

1 个答案:

答案 0 :(得分:13)

它与Context关联的主题有关。使用this正在使用与应用程序上下文具有不同主题的上下文(我假设您的ActivityFragment)。

如果您有理由需要使用应用程序上下文,可以将其包装在您的活动使用的任何主题中(通常在AndroidManifest.xml中设置)并且应该显示" round&# 34;烤面包。

Toast.makeText(new ContextThemeWrapper(getApplicationContext(), R.style.AppTheme), "This is a toast", Toast.LENGTH_SHORT).show();
相关问题