简体   繁体   中英

android - Toast text gets cut off

Coming across a weird issue with Toast - I have a few in my app to notify the user when they perform certain actions, and one in particular is having a display issue. It's fine on the emulator, and it's fine on my co-developer's android phone, but on my phone the second line of text gets cut off. The toast appears as a single line box and you can see the top of the second line of text that's getting cut off. Anyone else come across this issue? Is it just a general bug? I haven't been able to find the problem with google!

msg = getString(R.string.notification_cancel_add);    
Toast.makeText(AddEditActivity.this, msg, Toast.LENGTH_SHORT).show();

and R.string.notification_cancel_add = <string name="notification_saved_message">Your Notification has been saved.</string>

It is very weird . But I have solved it by passing the application context.

msg = getString(R.string.notification_cancel_add);    
Toast.makeText("pass aplication class context", msg, Toast.LENGTH_SHORT).show();

It will work brother.

You have to use the application context, getContext or MyClass.this In an Activity, use getApplicationContext()

As said in the developer documentation :

First, instantiate a Toast object with one of the makeText() methods. This method takes three parameters: the application Context, the text message, and the duration for the toast.

Source : https://developer.android.com/guide/topics/ui/notifiers/toasts

Check out android localization futures, here . Maybe you have to code extra for your phone specifics (display, etc...). Like you said, it works fine with other devices

当您将String硬编码到Toast时,它还会切断文本吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM