在Android中我想在屏幕底部显示一条toast消息,我尝试了这个:
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).show();
不起作用,我该如何正确操作?
Android 11 之后不适用:
在屏幕中央显示 Toast。
Toast toast = Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
在 Android 11 之前,要将 Toast 居中,请使用:
科特林:
val toast = Toast.makeText(context, "Test", Toast.LENGTH_LONG)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
Java:
Toast toast = Toast.makeText(context, "Test", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
或自定义视图。
警告:从 Android 11(R、API 30)开始,您无法在 Android 11 及更高版本上显示居中的 Toast:
找到了制作自定义吐司的解决方案。 警告:我不知道这种方式是否是一个好的做法。
让我们看看它是如何在 Android Studio 中制作的,尽管它的 Custom Toast 完全可以修改。 新建一个布局文件例如:toast_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="10dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#2d2d2d"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the custom toast"
android:textSize="20dp"
android:textColor="@color/white"
android:layout_margin="10dp"
/>
</androidx.cardview.widget.CardView>
</LinearLayout>
在您的 .java 文件中(无论您想在哪里显示自定义 Toast):
View v = View.inflate(this, R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout));
Toast toast = new Toast(MainActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
并且,您的自定义吐司将如下所示:
自定义 toast 的布局文件
.java 文件用于按钮点击事件上的自定义 toast
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Button is clicked!");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
toast.show();
在 koltin 中显示/设置文本重力在中心(水平)
以下代码可用于显示Toast消息
对于科特林;
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).apply{
setGravity(Gravity.CENTER, 0, 0)}.show()
自定义 Toast 在 Android 11 及更高版本(API 30 及更高版本)中已弃用。因此,如果您仍想使用自定义 Toast,请选择 Android 10 及以下版本或 API 29 及以下版本的 AVD 配置。
请使用此线路:
这不适用于 Android 11
下面的代码对我有用。