Android 在定制的烤面包上设置边距

Android 在定制的烤面包上设置边距,android,toast,Android,Toast,我正在显示一个定制的烤面包片,需要在其上应用左右边距。到目前为止,我一直在尝试: 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);

我正在显示一个定制的烤面包片,需要在其上应用左右边距。到目前为止,我一直在尝试:

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();
}
重力。水平填充
使土司填充整个布局宽度

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>


由于我使用的是
FILL\u HORIZONTAL
属性,因此指定一个确定的布局宽度(例如100dp)也没有效果。如何在自定义布局上实现边距?

可能有点离题,但您的自定义
Toast
看起来您可以直接使用它

如果您想在具有填充水平属性的自定义Toast中添加边距,您只需使用两个嵌套的ConstraintLayout-s,并在外部布局中添加填充即可。 还要确保将id标识符添加到根布局中

my 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()