这是我工具栏的布局,如果有帮助的话
<androidx.appcompat.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:paddingStart="0dp"
android:paddingEnd="10dp"
android:splitMotionEvents="false"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintBottom_toTopOf="@id/scrollable"
app:popupTheme="@style/ThemeOverlay.AppCompat.DayNight" />
首先为您想要的自定义Toast视图制作一个xml文件(在这种情况下,我认为仅拥有一个TextView就足够了:]
XML文件名为view_custom_toast
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#AA000000"
android:setTextColor="#ffffff" />
在MainActivity
中像这样膨胀您的自定义视图并在其上设置文本:
TextView toastTextView = (TextView) getLayoutInflater()
.inflater.inflate(R.layout.view_custom_toast,
(ViewGroup) findViewById(R.id.view_custom_toast));
toastTextView.setText("Hello World!");
最后,您应该将此视图设置为这样:
Toast toast = new Toast(getApplicationContext);
toast.setView(toastTextView);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();