直接上代码
说的都在代码里自行阅读
- MainActivity
package com.hrk.toast;
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.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
// 最简单的就不用说了
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
// 先不要show
Toast toast1 = Toast.makeText(this, "1", Toast.LENGTH_SHORT);
// 三个分别为Gravity样式,x轴偏移量和y轴偏移量
toast1.setGravity(Gravity.CENTER, 0, 0);
toast1.show();
break;
case R.id.btn3:
Toast toast2 = Toast.makeText(this, "1", Toast.LENGTH_SHORT);
// 这里的R.layout.view_toast布局可以很复杂,但是!!!因为toast是个轻量级的所以还是不建议写的太复杂
// 我这里只写了一个ImageView
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_toast, null);
// 这里注意findViewById(R.id.IV)是谁的IV
ImageView imageView = view.findViewById(R.id.IV);
imageView.setImageResource(R.drawable.ic_launcher_background);
toast2.setView(view);
// 这里可以单独设置时常
toast2.setDuration(Toast.LENGTH_SHORT);
toast2.show();
break;
}
}
}
- 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:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="移动位置" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加布局" />
</LinearLayout>
- view_toast.xml
还有最后一个自定义布局的布局
当然这个不巨野可以不写,直接在Activity里面设置也行。。。。
<?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">
<ImageView
android:id="@+id/IV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
以上在添加布局的时候最底层的布局(也就是我这里的LinearLayout)大小永远也不会生效如果想设置大小需要在添加一层Layout如下所示
<LinearLayout...>
<LinearLayout...(这里设置大小)>
内容
</LinearLayout>
</LinearLayout>
下面是toast封装
其次在使用中多次点击一个button弹出toast时如果toast还没有消失就再次点击会导致多个toast排队逐一显示那怎么解决呢?
- 有的手机厂商已经优化会覆盖新的toast
- 高版本的手机也有可能已经优化
- 一商量种情况看情况跳过
可以包装一下toast像下面
public class MyToast {
private Toast toast;
void show(Context context, String text, int duration) {
if (toast == null) {
toast.makeText(context, text, duration);
}else
toast.setText(text);
toast.show();
}
}
其实继承toast重写show方法我觉得更好。。。。