Android Custom toast Width cannot be set problem resolved

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.