android - 自定义吐司不显示消息
问题描述
我创建了一些自定义吐司布局,但遇到了问题!
我的自定义吐司没有显示我的消息并显示
这是我的布局代码:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_custom_toast">
<TextView
android:id="@+id/tv_toast_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical|center_horizontal"
android:lineSpacingExtra="4sp"
android:singleLine="false"
android:textAlignment="center"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_toast"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast " />
<ImageView
android:id="@+id/iv_toast"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_error_outline_red_24dp"
tools:ignore="ContentDescription" />
这是我的烤面包机功能代码:
public void toaster(String message, int length, int form) {
Toast toast = new Toast(context);
if (toastLayout == null) {
toastLayout = LayoutInflater.from(context).inflate(R.layout.custom_toast, null);
}
TextView toastMessage = toastLayout.findViewById(R.id.tv_toast_message);
toastMessage.setText(message);
toastMessage.setTypeface(MyApplication.getIranianSansFont(context));
ImageView toastImage = toastLayout.findViewById(R.id.iv_toast);
if (form == SUCCESS) {
toastImage.setImageResource(R.drawable.ic_check_circle_green_24dp);
toastMessage.setTextColor(context.getResources().getColor(R.color.greenSuccess));
} else if (form == ERROR) {
toastImage.setImageResource(R.drawable.ic_error_outline_red_24dp);
toastMessage.setTextColor(context.getResources().getColor(R.color.redError));
}
toast.setDuration(length);
toast.setView(toastLayout);
toast.show();
}
我的文本视图高度应该是包装内容,宽度是匹配约束,但是当我将它们都设置为包装内容时,问题就消失了,除了吐司的视觉之外,一切都很好!吐司布局粘在屏幕上,它的边距被删除,消息文本在图像视图
下有谁能帮我解决我的问题吗?
解决方案
我刚刚使用了 Linear Layout 而不是 Constraint Layout ,这是 Constraint Layout 有问题!
我的线性布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/selector_custom_toast">
<TextView
android:id="@+id/tv_toast_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:lineSpacingExtra="4sp"
android:singleLine="false"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textDirection="rtl"
tools:text="Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast Test Toast " />
<ImageView
android:id="@+id/iv_toast"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="end|center_vertical"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
app:srcCompat="@drawable/ic_error_outline_red_24dp"
tools:ignore="ContentDescription" />
推荐阅读
- reactjs - 材质 UI 图像渲染延迟
- c++ - Unreal Engine 4.26.1 - 随机化行为树并通过 BP 或 C++ 创建行为树
- oauth-2.0 - 将 Open ID Connect 集成到 Web 应用程序
- json - AssemblyScript MessagePack (MsgPack) 序列化/反序列化
- mysql - 我可以决定在左连接上有 x 空行吗?
- sass - 在 vue 3 中使用 sass
- nlp - PyTorch:最小的工作示例或教程,展示如何在训练和推理模式下使用 nn.TransformerDecoder 进行批量文本生成?
- c++ - Qt递归单次定时器似乎会导致内存泄漏
- mysql - 无法在 Ubuntu 20.04 上卸载 mysql
- erlang - 是否有可用于 rebar3 的插件路径自定义?