【问题标题】:How to make custom toast message to take the whole screen如何使自定义吐司消息占据整个屏幕
【发布时间】:2011-10-12 10:17:41
【问题描述】:

我为自定义 toast 消息创建了布局,并将 fill_parent 设置为自定义布局的根元素,但布局的大小仍然比整个屏幕小很多。

是否可以设置toast消息的大小占据整个屏幕?

<?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>

注意 android:layout_width="fill_parent" 和 android:layout_height="fill_parent" 属性。但我的布局仍然是屏幕的三分之一左右。 . .

任何想法或建议如何让我吐司占据整个屏幕?

【问题讨论】:

    标签: android layout android-layout customization toast


    【解决方案1】:
    toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.FILL_VERTICAL, 0, 0);
    

    【讨论】:

      【解决方案2】:

      认为你需要在调用 toast 时设置这个:

      Toast myToast = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT);
      myToast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
      

      不记得您需要将管道字符放在哪里,然后添加 Gravity.FILL_VERTICAL。

      【讨论】:

        【解决方案3】:

        toast.setGravity(Gravity.FILL,0,0)这一行,它让吐司随心所欲地填满整个屏幕。这是我用于自定义 toast 的,我想填满整个屏幕。

        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()
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多