Как настроить тост в Android
Я новичок в Android и работаю над примером приложения. Я хочу узнать, как мы можем настроить Android Toast по умолчанию.
Я хочу изменить цвет, стиль и другие атрибуты Toast.
Можно ли добавить изображение в Toast?
Я прочитал следующую запись в stackOverflow
Как настроить тост в Android?.
настроить тост в android
но ничто из этого не объясняет, как добавить изображение в Toast.
Ответы
Ответ 1
Да, мы можем изменить цвет, размер, положение и другие атрибуты Toast.
Мы также можем добавить изображение к тосту.
Хороший блог для этого Как настроить тост в Android Все содержимое взято из этого блога
Вы можете создать XML и раздуть его Toast.
Вы также можете сделать это во время выполнения
LinearLayout layout=new LinearLayout(this);
layout.setBackgroundResource(R.color.LightOrange);
TextView tv=new TextView(this);
// set the TextView properties like color, size etc
tv.setTextColor(Color.RED);
tv.setTextSize(15);
tv.setGravity(Gravity.CENTER_VERTICAL);
// set the text you want to show in Toast
tv.setText("My Custom Toast at Bottom of Screen");
ImageView img=new ImageView(this);
// give the drawble resource for the ImageView
img.setImageResource(R.drawable.myimage);
// add both the Views TextView and ImageView in layout
layout.addView(img);
layout.addView(tv);
Toast toast=new Toast(this); //context is object of Context write "this" if you are an Activity
// Set The layout as Toast View
toast.setView(layout);
// Position you toast here toast position is 50 dp from bottom you can give any integral value
toast.setGravity(Gravity.BOTTOM, 0, 50);
toast.show();
Ответ 2
LayoutInflater inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.cred_menu_like_popup, (ViewGroup)
activity.findViewById(R.id.like_popup_layout));
ImageView imageView = (ImageView) layout.findViewById(R.id.like_popup_iv);
TextView text = (TextView) layout.findViewById(R.id.like_popup_tv);
text.setText("Like");
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 200);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Вот макет
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/like_popup_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/customshapetransparent"
android:paddingTop="35dp"
android:paddingBottom="25dp"
android:paddingLeft="35dp"
android:paddingRight="35dp"
>
<ImageView
android:id="@+id/like_popup_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/like_popup_tv"
android:layout_below="@id/like_popup_iv"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="20sp"
/>
</RelativeLayout>
Пользовательская форма макета:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#60000000" />
<corners android:radius="8dp" />
</shape>
Ответ 3
Чтобы настроить форму и цвет тоста, используйте это.
Toast toast = Toast.makeText(getApplicationContext(), "You not Subscribe Try again", Toast.LENGTH_LONG);
View vieew = toast.getView();
// vieew.setBackgroundColor(Color.parseColor("#BD8BDC"));
vieew.setBackgroundResource(R.drawable.textinputborder);
toast.setView(vieew);
toast.show(); //This displays the toast for the specified lenght.
Также используйте в R.drawable.textinputborder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#83BB66" />
<stroke android:width="1dp"
android:color="#1B200A" />
<corners android:radius="20dp" />
</shape>
</item>
</selector>
Ответ 4
Сначала создайте свой настраиваемый интерфейс... для простоты я настраиваю пользовательский интерфейс, как показано ниже:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/CustomToastLayoutRoot"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/close"
android:id="@+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Warning !!!"
android:id="@+id/textView"
android:layout_gravity="bottom" />
</LinearLayout>