我想在Android应用中在屏幕底部显示一个Toast信息,我尝试了以下代码:
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).show();
它不起作用,我该怎么正确操作?
我想在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();
setGravity()
应该返回 Toast 以便能够链式调用。哦,错过了机会! - doctorram在 Android 11 之前,要将 Toast 居中,可以使用以下代码:
Kotlin:
val toast = Toast.makeText(context, "Test", Toast.LENGTH_LONG)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
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:
<?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>
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将会看起来像这样:
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
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 = Toast.MakeText(ApplicationContext, "bbb", ToastLength.Long);
toast.SetGravity(GravityFlags.Center, 0, 0);
toast.Show();
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
toast.show();
fun Context.longToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_LONG)
.apply {
view.findViewById<TextView>(android.R.id.message)?.gravity = Gravity.CENTER
}
.show()
}
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).apply{
setGravity(Gravity.CENTER, 0, 0)}.show()
你可以用一行代码完成这个操作。
对于 Kotlin;
val toast = Toast.makeText(this,"Yes clicked...",Toast.LENGTH_LONG)
toast.setGravity(Gravity.CENTER,0,0)
toast.show()
Toast tt = Toast.makeText(MainActivity.this,"Your text displayed here", Toast.LENGTH_LONG);
tt.setGravity(Gravity.CENTER, 0, 0);
tt.show();