如何更改在长按动作栏项目上自动显示的吐司风格

问题描述 投票:0回答:1

当前,长按操作栏项目时显示的祝酒看起来不太好this is how it is

我如何赋予它类似的外观(在按钮周围并靠近按钮有一些填充)?whatsapp

甚至null

这是我工具栏的布局,如果有帮助的话

<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" />
android kotlin android-toast
1个回答
0
投票

首先为您想要的自定义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();
© www.soinside.com 2019 - 2024. All rights reserved.