【问题标题】:Toast message text alignment out of boxToast 消息文本对齐方式开箱即用
【发布时间】:2020-04-22 13:54:17
【问题描述】:

我在 postexecute 上显示来自 async-task 的 toast 消息,但它显示不正确,如下图所示。 任何人都可以帮助我。

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

【问题讨论】:

  • 首先,粘贴你的代码,当你在不同的位置和不同的设备上调用它时,Toast 是否正常工作?
  • @EliasFazel 在某些设备上可以正常工作

标签: android android-asynctask android-toast


【解决方案1】:

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

请查看此链接 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();

【讨论】:

  • 不错的解决方案,但系统 Toast 视图有什么问题?
猜你喜欢
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
相关资源
最近更新 更多