dialog上弹出toast

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);
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值