效果:
点击按钮“show toast dialog”,弹出底部提示框“toast 提示信息”,2s后提示框自动消失。
values文件中styles.xml
<!-- ToastDialog -->
<style name="ToastDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> <!-- 是否有边框 -->
<item name="android:windowIsFloating">true</item> <!-- 是否是浮动 -->
<item name="android:windowContentOverlay">@null</item> <!-- 是否有覆盖 -->
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <!-- 动画样式 -->
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> <!-- 窗口软键盘的交互模式 -->
<item name="android:windowBackground">@android:color/transparent</item> <!-- 背景颜色 -->
<item name="android:windowNoTitle">true</item> <!-- 是否有标题 -->
</style>
drawable文件夹中shape_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#BF000000" />
<corners android:radius="20dp" />
</shape>
layout文件夹中dialog_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_corner">
<TextView
android:id="@+id/id_tv_show_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textColor="@android:color/white"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="show msg" />
</androidx.constraintlayout.widget.ConstraintLayout>
ToastDialog.class
public class ToastDialog extends Dialog {
private static ToastDialog toastDialog = null;
public TextView tv_show_toast;
public Handler timeHandler = null;
/**
* 构造函数
*
* @param context
* @param themeResId
*/
public ToastDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
/**
* @param context
* @param showMsg 展示内容
* @param showTime 展示时长
* @return
*/
public static void showToastDialog(Context context, String showMsg, long showTime) {
toastDialog = new ToastDialog(context, R.style.ToastDialog);
toastDialog.setContentView(R.layout.dialog_toast);
//布局位置
toastDialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
toastDialog.getWindow().getAttributes().y = 50;
//去掉遮罩层(全透明)
toastDialog.getWindow().setDimAmount(0f);
//绑定控件
toastDialog.tv_show_toast = toastDialog.findViewById(R.id.id_tv_show_toast);
toastDialog.timeHandler = new Handler();
//展示内容
toastDialog.tv_show_toast.setText(showMsg);
//展示时长
toastDialog.show();
toastDialog.timeHandler.postDelayed(new Runnable() {
@Override
public void run() {
toastDialog.dismiss();
}
}, showTime);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
findViewById(R.id.id_btn_show_toast_dialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastDialog.showToastDialog(mContext, "toast 提示信息", 2000);
}
});
}
}