Android Toast
时间:2020-02-23 14:29:21 来源:igfitidea点击:
Toast消息对在Android应用中显示一小段时间的通知很有用。
在本教程中,我们将讨论并实现android Toast消息示例。
Android Toast
默认情况下,Android Toast用于在屏幕底部显示特定时间间隔的通知。
Toast消息不会阻止用户交互,并且在超时后自动消失。
android.widget.Toast类是java.lang.Object类的子类。
创建基本的Toast消息
Android Toast消息是使用方法" makeText()"创建的,该方法随上下文,消息和持续时间一起传递,如下所示:
Toast toast = Toast.makeText(context, "String goes here", duration);
"上下文"可以是应用程序或者活动的上下文。
建议使用getApplicationContext()来显示Toast,而与Activity的当前状态无关。
持续时间可以设置为Toast.LENGTH_SHORT或者Toast.LENGTH_LONG。
使用方法show()显示Toast。
Toast.makeText(getApplicationContext(),"Basic Toast message", Toast.LENGTH_SHORT).show()
定位您的吐司消息
屏幕底部显示一条标准的Toast消息。
我们可以在Toast上设置自己的重力,如下所示
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
第二个和第三个参数用于将烤面包分别向右和向下移动指定的偏移量。
Toast Android的自定义布局
要创建自定义布局,我们可以在XML中定义视图布局,例如说" custom_toast.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="#7DA1BC"
>
<ImageView android:src="@android:drawable/stat_notify_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:textColor="#FFF"
</LinearLayout>
在活动类中,我们将对上述布局进行充气,并使用setView()方法在Toast上进行设置。
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = layout.findViewById(R.id.text);
text.setText("This is a custom toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
让我们创建一个应用程序,其中每个按钮将显示刚刚讨论过的各种Toast消息。
Android Toast消息代码
下面给出了" activity_main.xml"布局的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
tools:context="com.theitroad.toasts.MainActivity">
<Button
android:id="@+id/btnBasicToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic Toast"
<Button
android:id="@+id/btnGravityToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic Toast With Gravity"
<Button
android:id="@+id/btnOffsetToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic Toast With Gravity And Offset"
<Button
android:id="@+id/btnCustomToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast"
</LinearLayout>
下面给出了MainActivity.java的代码
package com.theitroad.toasts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnToast, btnGravityToast, btnOffsetToast, btnCustomToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnToast = findViewById(R.id.btnBasicToast);
btnGravityToast = findViewById(R.id.btnGravityToast);
btnOffsetToast = findViewById(R.id.btnOffsetToast);
btnCustomToast = findViewById(R.id.btnCustomToast);
btnToast.setOnClickListener(this);
btnGravityToast.setOnClickListener(this);
btnOffsetToast.setOnClickListener(this);
btnCustomToast.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnBasicToast:
Toast.makeText(getApplicationContext(), "Basic Toast", Toast.LENGTH_SHORT).show();
break;
case R.id.btnGravityToast:
Toast toast = Toast.makeText(getApplicationContext(), "Toast with Gravity", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
break;
case R.id.btnOffsetToast:
toast = Toast.makeText(getApplicationContext(), "Toast With Offset", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 50, 50);
toast.show();
break;
case R.id.btnCustomToast:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = layout.findViewById(R.id.text);
text.setText("This is a custom toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
break;
}
}
}

