Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中的活动中在特定位置显示自定义Toast_Java_Android - Fatal编程技术网

Java 如何在Android中的活动中在特定位置显示自定义Toast

Java 如何在Android中的活动中在特定位置显示自定义Toast,java,android,Java,Android,我需要在android上展示一个定制的祝酒词。将在特定位置显示。如何在屏幕的特定位置显示自定义烤面包。您可以使用此库 超级多士图书馆 来自 首先获取应用程序/活动上下文,定义消息字符串和持续时间,创建一个新的Toast对象,并将上下文、文本消息和持续时间传递给它,然后调用show() 您可以使用setGravity函数定位土司,该函数在toast.show()之前调用: 尝试使用这种方法 public static void showBottomToast(Context context, St

我需要在android上展示一个定制的祝酒词。将在特定位置显示。如何在屏幕的特定位置显示自定义烤面包。

您可以使用此库

超级多士图书馆

来自

首先获取应用程序/活动上下文,定义消息字符串和持续时间,创建一个新的Toast对象,并将上下文、文本消息和持续时间传递给它,然后调用show()

您可以使用setGravity函数定位土司,该函数在toast.show()之前调用:

尝试使用这种方法

public static void showBottomToast(Context context, String msg) {
    View custom_view = LayoutInflater.from(context).inflate(
            R.layout.dialog_bottom_toast, null);

    TextView tvMessage = (TextView) custom_view
            .findViewById(R.id.tvMessage);
    tvMessage.setText(msg);

    showToast(context.getApplicationContext(), Gravity.FILL_HORIZONTAL
            | Gravity.BOTTOM, custom_view, Toast.LENGTH_LONG);
}

public static void showToast(Context ctx, int gravity, View root,
        int duration) {
    final Toast mToast = new Toast(ctx);
    mToast.setGravity(gravity, 0, 0);
    mToast.setDuration(duration);
    mToast.setView(root);
    mToast.show();

}
对话框_bottom_toast.xml是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:layout_margin="@dimen/marg_20"
    android:text="@string/welcome"
    android:maxLines="2"
    android:ellipsize="end"
    android:textColor="@color/blue"
    android:textSize="@dimen/sp_16"
    android:textStyle="bold" />

</LinearLayout>

实现自定义Toast的非常简单的方法

//show custom Toast in android
    private void showCustomToast(String showToast) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(showToast);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
将其布局定义为
toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@color/primary"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="hello"
        android:textSize="15sp"
        android:textColor="#ffffff" />
</LinearLayout>
1.使用文本视图创建一个布局自定义_toast.xml,如下所示
我喜欢你的回答。非常感谢。作为一个建议,如果你想在不同的位置展示土司,你可以参考这个
//show custom Toast in android
    private void showCustomToast(String showToast) {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_layout,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(showToast);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="@color/primary"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="hello"
        android:textSize="15sp"
        android:textColor="#ffffff" />
</LinearLayout>
 showCustomToast("Your_Message"); 
    <TextView
    android:id="@+id/textView1"
    android:layout_width="800dp"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_tost_shape"
    android:gravity="center_vertical|center_horizontal"
    android:text="Action Successfuly Completed!"
    android:textColor="#ff0000" 
    android:textSize="40sp"/>
LayoutInflater layoutInflater = GenApplicationId.this.getLayoutInflater();
             View view = layoutInflater.inflate(R.layout.custom_toast, null);
             TextView tvMessage = (TextView) view.findViewById(R.id.textView1);
             tvMessage.setHeight(100);
             tvMessage.setText(message);
             Toast toast = new Toast(getApplicationContext());
             toast.setDuration(Toast.LENGTH_LONG);
             toast.setGravity(Gravity.TOP, -0, 230);
             toast.setView(view);
             toast.show();