我正在显示一个自定义吐司,需要在其上应用左右边距。到目前为止,这是我尝试过的:
JAVA
public void showCustomToast() {
Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP|Gravity.FILL_HORIZONTAL, 0, 100);
toast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);
if (toast != null) toast.show();
}
Gravity.FILL_HORIZONTAL
使toast填充整个layout_width。
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp"
android:layout_marginStart="200dp"
android:layout_marginEnd="20dp">
<ImageView
android:id="@+id/imgTest"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/test"
android:rotation="-50"
android:src="@drawable/test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:letterSpacing=".1"
android:text="@string/test"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
指定一个明确的layout_width,例如100dp也没有效果,因为我使用的是FILL_HORIZONTAL
属性。如何在自定义布局上实现边距?
也许有点offtopic但你的自定义Toast
看起来你可以只使用Snackbar