dialog上弹不出toast怎么办,没办法只好写一个模仿toast功能的:
原理:弹出一个新的dialog,里面有要提示的信息,然后弄一个计时器,dismiss这个dialog。如下:
这是dialog的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_toast"
android:gravity="center_horizontal|bottom"
android:padding="8dp" >
<TextView
android:id="@+id/dialog_alert"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/common_white"
android:textSize="@dimen/textsize_normal"/>
</LinearLayout>
这是弹出dialog的样式:
<style name="DialogToast" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<!-- Dialog 弹出不变暗 -->
<item name="android:backgroundDimEnabled">false</item>
</style>
这是dialogtoast工具类:
public class DialogToastUtil
{
Dialog dialog = null;
public void showDialogToast(Context mContext,int resId)
{
showDialogToast(mContext, mContext.getResources().getString(resId));
}
public void showDialogToast(Context mContext, CharSequence str)
{
dialog = new Dialog(mContext, R.style.DialogToast);
Window window = dialog.getWindow();
window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
WindowManager.LayoutParams lp = window.getAttributes();
lp.y = 300;
window.setAttributes(lp);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(R.layout.item_dialog);
TextView tv = (TextView) window.findViewById(R.id.dialog_alert);
tv.setText(str);
dialog.show();
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
dialog.dismiss();
}
}, 2000);
}
}