Toast三种方式的写法
1.布局文件(activity_main)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button01"
android:text="我是一个使用MakeText()弹出的Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button02"
android:text="我是自定义的Toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button03"
android:text="XMl布局"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
toast.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提示" />
<TextView
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="网络有异常" />
</LinearLayout>
</LinearLayout>
2.MainActivity代码
package com.example.android0010;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import static android.widget.Toast.makeText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private Button button2;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=findViewById(R.id.button01);
button1.setOnClickListener(this);
button2=findViewById(R.id.button02);
button2.setOnClickListener(this);
button3=findViewById(R.id.button03);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button01: //普通的
makeText(MainActivity.this,R.string.app_name,Toast.LENGTH_SHORT).show();
case R.id.button02: //自定义的
// Toast toast=new Toast(MainActivity.this); //构造方法
// Toast toast= Toast.makeText(MainActivity.this,null,Toast.LENGTH_SHORT).show();
//设置显示位置
// toast.setGravity(Gravity.CENTER,0,0);
// toast.setText("我是自定义的Toast");
// //显示
// toast.show();
//通过构造方法创建一个Toast
Toast toast=new Toast(MainActivity.this);
//设置显示时间
toast.setDuration(Toast.LENGTH_SHORT);
//建立一个LinearLayout
LinearLayout ll=new LinearLayout(MainActivity.this);
//放图片
ImageView iv=new ImageView(MainActivity.this);
iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_background));
//建立一个TextView
TextView tv=new TextView(MainActivity.this);
tv.setText("我是自定义的Toast");
tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher_foreground));
ll.addView(tv);
toast.setView(ll); //把自己写的布局放到Toast
toast.show();
case R.id.button03:
//创建一个Toast对象
Toast toast1=new Toast(MainActivity.this);
//设置时间
toast1.setDuration(Toast.LENGTH_SHORT);
//设置显示位置
toast1.setGravity(Gravity.CENTER,0,0);
//得到一个LayoutInflate的对象
LayoutInflater inflater=getLayoutInflater(); //这是MainActivity他的子类
View view=inflater.inflate(R.layout.toast,null); //获取toast布局
toast1.setView(view);
toast1.show();
}
}
}