创建全屏自定义Toast

14

我最近尝试了一个自定义的Toast,参照了以下教程:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

使用了如下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

但是在根布局中设置fill_parent似乎没有效果。

你有什么办法可以解决这个问题,以便获得全屏的Toast?

2个回答

23

在显示 Toast 之前添加以下代码:

toast.setGravity(Gravity.FILL, 0, 0);

3
为了让 toast 完全填充其容器的水平和垂直尺寸,您需要使用 Yoah 答案中提到的 Gravity.FILL。我尝试了以下代码并且它可以工作。
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL, 0, 0);
toast.setView(view); //inflated view
toast.setDuration(Toast.LENGTH_LONG);
toast.show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接