How to Customise Toast in Android?

一个人想着一个人 提交于 2019-12-30 06:17:10

问题


Is it possible to make Customize Toast in Android. like if can we place in it image icon and place button.


回答1:


You can also use the regular makeText() and handle the getView() to set an image next to see the next.

Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));



回答2:


You can put any view in a Toast using setView. However, I'm not quite sure why you would want to place a button in it, as a Toast will rapidly disappear. Taken from the officiel developer site :

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see.

So the toast should only be used to display information. For more complex interactions, you can use a Dialog.




回答3:


Toast is non focus able.Adding button did not make sense. However you can display information.You can also control its visibility means u can hide and show by making few changes in Toast class.




回答4:


It is obviously possible to create custom toast in android. Just check my blog.http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/ I've explained about it in detail.




回答5:


XML FILE

enter code here`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="8dp"
          android:background="#DAAA"
          >
<ImageView android:src="@drawable/droid"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="8dp"
           />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />

'

JAVA CODE

 LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();



回答6:


toast can be customized to show at different places like top, bottom, center, left, right

 toast.setGravity(Gravity.TOP , 0, 0); // for example setting gravity to top

for more details [here](http://androidcoding.in/2016/05/31/android-tutorial-custom-toast/"android custom toast")



来源:https://stackoverflow.com/questions/6562034/how-to-customise-toast-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!