android toast doesn't fit text

后端 未结 2 735
一向
一向 2021-02-15 03:48

I am developing an application where I have to use numerous toasts.

I display these toasts by using:

Toast.makeText(context, \"Some medium-sized text\",          


        
2条回答
  •  名媛妹妹
    2021-02-15 04:39

    Using the main idea of this Custom toast in android : a simple example and this Android's Toast default colors and alpha I developed a simple custom Toast that looks like the default one but it wraps the text in multilines.

    I create a simple class with the makeText(context,text,duration) static method, so I had only to replace Toast.makeText with CustomToast.makeText everywhere in my projects.

    Below the code

    CustomToast.java

    public class CustomToast extends Toast{
        /**
         * Construct an empty Toast object.  You must call {@link #setView} before you
         * can call {@link #show}.
         *
         * @param context The context to use.  Usually your {@link Application}
         *                or {@link Activity} object.
         */
        public CustomToast(Context context) {
            super(context);
        }
    
        public static Toast makeText(Context context, CharSequence text, int duration) {
            Toast t = Toast.makeText(context,text,duration);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View layout = inflater.inflate(R.layout.custom_toast,null);
    
            TextView textView = (TextView) layout.findViewById(R.id.text);
            textView.setText(text);
    
            t.setView(layout);
    
    
            return t;
        }
    
    }
    

    The layout layout/custom_toast.xml

     
    
    
        
    
    
    

提交回复
热议问题