![](https://i-blog.csdnimg.cn/blog_migrate/1cfd19faf9aad301025d27ae794b4dca.png)
android 自定义吐司
Here you will learn how to create custom toast in android with some text and image.
在这里,您将学习如何在Android中使用一些文本和图像创建自定义吐司。
Toast is used to show some information for specific time period. In android we use android.widget.Toast class to make toast message.
吐司用于显示特定时间段内的某些信息。 在android中,我们使用android.widget.Toast类制作Toast消息。
A normal toast message with some text can be created by following code.
可以通过以下代码创建带有一些文本的普通吐司消息。
//toast message for short time period
Toast.makeText(getApplicationContext(), "Simple Toast example.", Toast.LENGTH_SHORT).show();
//toast message for long time period
Toast.makeText(getApplicationContext(), "Simple Toast example.", Toast.LENGTH_LONG).show();
Android provide facility to customize toast message. Like if we want a toast message with some text and image. It can be done in following way.
Android提供了自定义吐司消息的功能。 就像我们想要带有一些文本和图像的祝酒消息一样。 可以通过以下方式完成。
Android自定义吐司示例 (Android Custom Toast Example)
1. Create an android project with package name thecrazyprogrammer.androidexample
1.使用包名称thecrazyprogrammer.androidexample创建一个android项目
2. Add an image in res/drawable folder. In this example I have used logo.png.
2.在res / drawable文件夹中添加图像。 在此示例中,我使用了logo.png 。
The project has following structure.
该项目具有以下结构。
![Android Custom Toast Example 1](https://i-blog.csdnimg.cn/blog_migrate/480a44b7fdc144d994e9b4a232f11859.png)
Add following code in respective files.
在相应的文件中添加以下代码。
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Here To Open Custom Toast"
android:onClick="clickAction"
android:id="@+id/btn"/>
</LinearLayout>
custom_toast.xml
custom_toast.xml
It is layout file that contains the code for custom toast.
这是包含自定义吐司代码的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:src="@drawable/logo"
android:layout_gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The Crazy Programmer"
android:gravity="center"/>
</LinearLayout>
MainActivity.java
MainActivity.java
package thecrazyprogrammer.androidexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
}
public void clickAction(View view){
LayoutInflater layoutInflater = getLayoutInflater();
View view1 = layoutInflater.inflate(R.layout.custom_toast,null);
Toast toast = new Toast(getApplicationContext());
toast.setView(view1);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
}
Here we are simply inflating the custom toast xml into Toast view. When you click on button a custom toast with text and image will pop up for some time. You can also do further customization like changing the text size, color, background, etc.
在这里,我们只是将自定义的Toast xml扩展到Toast视图中。 当您单击按钮时,带有文本和图像的自定义吐司将弹出一段时间。 您还可以进行进一步的自定义,例如更改文本大小,颜色,背景等。
Output
输出量
![Android Custom Toast Example 2](https://i-blog.csdnimg.cn/blog_migrate/a82869f310bb4e1650cc21bd4fc0a43b.png)
If you are facing any difficulty then you can ask it in comment section.
如果您遇到任何困难,可以在评论部分提出。
翻译自: https://www.thecrazyprogrammer.com/2016/07/android-custom-toast-example.html
android 自定义吐司