有的时候项目Toast不符合系统自带Toast样式,这个时候我们就需要简单封装一下系统Toast.这里不是自定义Toast只是对系统Toast简单封装方便使用.
1.Toast封装.
public static void myToast(String text){
Toast toast = Toast.makeText(App.Companion.getContext(), text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);//改变弹出位置
toast.show();
}
public static void myToast(String text, Context context){
View view= View.inflate(context, R.layout.toast,null);//编写自己xml布局
TextView textView=view.findViewById(R.id.text);
textView.setText(text);
Toast toast = new Toast(App.Companion.getContext());
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
2.Toast中自定义自己布局文件toast.xml.简单方便画出自己想要布局.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:background="@drawable/bg23"
android:textSize="@dimen/sp_15"
android:paddingLeft="@dimen/dp_20"
android:paddingRight="@dimen/dp_20"
android:textColor="@color/white"
android:text="我是Toast信息"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_46"/>
</LinearLayout>
项目咨询请联系我们