I created layout for my custom toast message, and I set fill_parent to the root element of my custom layout but the size of the layout is still a lot smaller than the whole screen.
Is it possible to set the size of the toast message to take the whole screen ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
开发者_运维百科 android:orientation="vertical" android:gravity="center" >
<ImageView android:id="@+id/image"
android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dip"/>
</LinearLayout>
Note the android:layout_width="fill_parent" and android:layout_height="fill_parent" properties. But still my layout is around one third of the screen . . .
any ideas or suggestions how can I make the toast to take the whole screen ?
toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.FILL_VERTICAL, 0, 0);
Think you need to set this when calling the toast:
Toast myToast = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
Can't remember exactly where you would need to put the pipe character to then add Gravity.FILL_VERTICAL.
Look at this line toast.setGravity(Gravity.FILL,0,0)
, it makes the toast fill the whole screen just as you want. It is what I use for custom toasts, which I would like to fill the whole screen.
fun showCorectAnswerToast() {
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.custom_toast,
view?.findViewById<View>(R.id.custom_toast_container) as ViewGroup?)
val toast = Toast(context)
toast.setGravity(Gravity.FILL,0,0)
toast.duration = Toast.LENGTH_SHORT
toast.view = layout
toast.show()
}
精彩评论