我已经创建了一些自定义的吐司布局,并且遇到了问题!
我的自定义吐司不显示我的消息,而是显示
这是我的布局代码:
<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();
}
我的文本视图高度应该是包裹内容,宽度是匹配约束,但是当我将它们都设置为包裹内容时,问题就消失了,并且一切都很好,除了吐司的视觉效果!吐司版图粘在屏幕周围,并且在图像视图下去除了页边距和消息文本
有人可以帮我解决我的问题吗?
答案 0 :(得分:1)
我只是使用线性布局而不是约束布局,所以约束布局出了点问题!
我的线性布局代码:
<?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" />