android Toast 内容显示两行
时间: 2024-10-18 19:06:35 浏览: 59
Android Toast 是一种小巧的弹出通知窗口,通常用于快速传递少量信息给用户。默认情况下,Toast 只能显示一行文本。如果你需要显示两行内容,可以自定义一个更大的布局视图并将其设置为 Toast 的内容。
首先,你需要创建一个包含两个 TextView 的 XML 文件,例如:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
```
然后,在 Java 或 Kotlin 代码中使用这个自定义布局:
```java
LayoutInflater inflater = LayoutInflater.from(context);
View toastLayout = inflater.inflate(R.layout.your_toast_layout, null);
TextView textView1 = toastLayout.findViewById(R.id.text_view_1);
textView1.setText("第一行文字");
TextView textView2 = toastLayout.findViewById(R.id.text_view_2);
textView2.setText("第二行文字");
Toast toast = new Toast(context);
toast.setView(toastLayout);
toast.setDuration(Toast.LENGTH_SHORT); // 设置显示时间
toast.show();
```
在这个例子中,`your_toast_layout` 是你在应用资源文件夹 `res/layout` 中为自定义 Toast 布局命名的 XML 文件。
阅读全文
相关推荐


















