Toast消息文本对齐开箱即用

问题描述 投票:0回答:2

我正在执行后的异步任务中显示吐司消息,但显示不正确,如下图所示。任何人都可以帮助我。

Toast.makeText(mContext, "Stamp Added Successfully"), Toast.LENGTH_LONG).show();

enter image description here

android android-asynctask android-toast
2个回答
0
投票

使用此

Toast.makeText(getApplicationContext(),“您的消息”,Toast.LENGTH_LONG).show();


0
投票

您可以创建自定义吐司消息。请点击以下链接。

请参阅此链接See more from here从此链接复制。

toast.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#DAAA" >

<ImageView android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginRight="10dp" />

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textColor="#FFF" />

MainActivity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout); 
toast.show();
© www.soinside.com 2019 - 2024. All rights reserved.