如何使自定义提示通知填充屏幕的宽度?
我尝试了多种方法,但都无法实现。
谢谢。
这应该可以工作:
Toast t = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT);
t.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
你可以使用自定义布局和fill_horizontal创建Toast。以下代码在适配器类中编写,并且运行良好。
LayoutInflater inflater =(LayoutInflater)mContext
.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
View layout =inflater.inflate(R.layout.custom_toast_layout,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(mContext);
//use both property in single function
toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
setGravity(Gravity.TOP or Gravity.FILL_HORIZONTAL, 0, 0)
。 - Jerry Okaforfinish()
。尝试像这样做...
wM = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
LayoutInflater inflate = (LayoutInflater) context
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = inflate.inflate(R.layout.custom_toast, null);
// Set the layout to be like a toast window!
mParams.gravity = Gravity.WHEREVER YOU WANT IT TO BE
mParams.height = 200;
mParams.width = WindowManager.LayoutParams.FILL_PARENT;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mParams.format = PixelFormat.OPAQUE;
mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(v);
<Timer or whatever>
wm.removeView(v);