In the project you want to implement a toast tip effect for a head, similar to
In the process of re-implementation, it was found that the width of the parent control directly by modifying the toast's view layout was not possible, and was later implemented by specifying the width of the textview inside the parent control directly with the code. Here is the specific code:
private static void makeToast(String msg) { if (toastView == null) { LayoutInflater inflater = (LayoutInflater) AppApplication.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE); params = new LinearLayout.LayoutParams(AppApplication.mScreenWidth, ViewGroup.LayoutParams.MATCH_PARENT); toastView = inflater.inflate(R.layout.toast_custom_prompt, null); } TextView tv = (TextView) toastView.findViewById(R.id.tvTitleToast); tv.setLayoutParams(params); tv.setText(msg); toast = new Toast(AppApplication.getInstance()); float hOffset = AppApplication.getInstance().getResources().getDimension(R.dimen.common_title_height); toast.setGravity(Gravity.TOP, 0, (int) hOffset); toast.setDuration(Toast.LENGTH_LONG); toast.setView(toastView); toast.show();}
The way I wrote it was a small way to calculate the width of the screen:
private void calcScreenSize() { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels; AppApplication.mScreenWidth = mScreenWidth; AppApplication.mScreenHeight = mScreenHeight;}
You can display a custom toast prompt below the title bar with the above steps
Android Custom toast Width cannot be set problem resolved