我正在显示自定义吐司,需要在其上施加左右边距。到目前为止,这是我尝试过的:
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
使吐司面包充满了整个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
属性。如何在自定义布局上实现页边距?
答案 0 :(得分:0)
也许有点题外话,但是您的自定义Toast
似乎可以用Snackbar
答案 1 :(得分:0)
如果要向具有FILL_HORIZONTAL属性的自定义吐司添加边距,则可以简单地使用两个嵌套的ConstraintLayout -s,并在外部布局中添加填充。 另外,请确保将ID标识符添加到根目录布局中。
toast_custom.xml的示例:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/toast_root_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="@dimen/large_margin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/custom_background">
<TextView
android:id="@+id/toast_message"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
然后在代码中初始化您的吐司对象:
val inflater = layoutInflater
val layout = inflater.inflate(R.layout.toast_custom,
findViewById(R.id.toast_root_layout))
val message = layout.findViewById<TextView>(R.id.toast_message)
message.setText("Your toast message")
val toast = Toast(applicationContext)
toast.setGravity(Gravity.TOP or Gravity.FILL_HORIZONTAL, 0, 0)
toast.duration=Toast.LENGTH_SHORT
toast.view=layout
toast.show()
最终结果。