How can I change default toast message color and background color in android?

后端 未结 15 889
执念已碎
执念已碎 2020-12-23 17:12

I want to create a toast message with background color is white and message color is black. My toast message is:

Toast.makeText(Logpage.this, \"Please Give          


        
15条回答
  •  被撕碎了的回忆
    2020-12-23 18:08

    Adding to @AndroidKiller's answer, you can also set the gravity and a custom TextView among other things like so:

    Toast toast = Toast.makeText(context, context.getResources().getString(resID), Toast.LENGTH_LONG);
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );        
    View toastView = li.inflate(R.layout.toast_hint_layout, null);
    TextView text = (TextView) toastView.findViewById(R.id.hint_text_tv);
    text.setText(resID);
    toast.setView(toastView);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 0);
    toastView.setBackgroundResource(R.drawable.toast_9_patch);          
    toast.show();
    

    Note, your background drawable should be a nine-patch PNG

    You can even put an ImageView, and multiple TextViews in with XML like this:

    
        
        
            
    
            
        
    
    

提交回复
热议问题