创建全屏自定义Toast

问题描述 投票:0回答:3
android fullscreen toast
3个回答
23
投票

在显示 Toast 之前添加此内容:

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

3
投票

要完全填满吐司容器的水平和垂直尺寸,您需要使用

Gravity.FILL,如 Yoah 的回答中所述。

我尝试遵循并且成功了。

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL, 0, 0);
toast.setView(view); //inflated view
toast.setDuration(Toast.LENGTH_LONG);
toast.show();

0
投票

您可以使用这种方法:

(自定义布局:toast_layout.xml)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#80000000" <!-- Semi-transparent black background -->
    android:gravity="center">

    <TextView
        android:id="@+id/textViewToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF" <!-- White text color -->
        android:textSize="18sp"
        android:textStyle="bold"
        android:padding="16dp"/>
</LinearLayout>

(处理这个Toast的后端)

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class FullScreenToast {

    public static void showFullScreenToast(Context context, String message) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.toast_layout, null);

        TextView textViewToast = layout.findViewById(R.id.textViewToast);
        textViewToast.setText(message);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}

(现在使用自定义Toast)

FullScreenToast.showFullScreenToast(this, "This is a full-screen toast!");
© www.soinside.com 2019 - 2024. All rights reserved.