问题
I wrote an activity which contains a navigation drawer (a simple fragment with a ListView inside), and a model and an adapter for the ListView. There is an ImageView and a TextView in a single list item, as it's usually be in many apps. That's all I have done, and the rest codes of the activity and fragment's classes were all generated automatically by Android Studio. Then when I used Toast.makeText
in the activity, the result looks as this.
I've searched for this question and tried using Toast.makeText(this.getApplicationContext(), ...)
instead of Toast.makeText(this, ...)
, and it worked. I'm wondering why would this happen and how should I solved this problem correctly?
回答1:
In the official documentation it says that a typical Toast looks like this:
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, text, duration);
I imagine it is because depending on the class you have, the context varies because not all context instances are created equal.
More info also here:
来源:https://stackoverflow.com/questions/24475333/why-is-the-width-of-toast-shorter-than-its-content