If one day, the whimsical PM wants you to achieve this prompting effect? Users click on the button --- a prompt message pops up, similar to the GIF effect shown above, how can we achieve it?
Of course, there are many ways, such as PopupWindow, or animation. In fact, there is another way we use most - Toast can also achieve this way, and it is simpler than other ways.Preface
Toast, the simplest and most common way of prompting in development, is generally simple to use.
Toast toast = Toast.makeText(this,"Tips!!!!", Toast.LENGTH_SHORT);
toast.show();
Is it too monotonous with a gray-black background and a hint?
So try a different Toast.
Change Toast's position
1. Setting Gravity
Toast toast = Toast.makeText(this,'Tip!!!! "Toast. LENGTH_SHORT";
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
To reset Gravity for Toast objects, besides setting CENTER, there are many locations to choose from, which can be set according to your needs.
2. Setting Margin:
toast.setMargin(0.5f,0.2f);3. Setting the display time
toast.setDuration(Toast.);
However, as long as Toast.LENGTH_LONG and LENGTH_SHORT are optional.
- 4. Reading the Toast source code, you will find that Toast has a way.
public void setView(View view)
What does that mean? We can set a View for Toast. That is to say, we can customize the style of Toast.
Do it.
Let's start with a layout:
"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="150dp"
android:background="@drawable/shape_bg"
android:orientation="vertical">
<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:padding="5dp"
android:src="@drawable/favorite" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="fancyToast"
android:textColor="@color/white" />
</LinearLayout>
Effect
Activity's layout is simple:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bt_toast"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:background="@color/colorPrimary"
android:text="Toast" />
</RelativeLayout>
A level-centered Button that initializes the control:
toast = new Toast(this);
View v = getLayoutInflater().inflate(R.layout.toast, null);
toast.setView(v);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
When you click Button, show Toast:
btToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toast.show();
}
});
Easy and efficacious.
According to Toast's setView() method, you can customize Toast's style. It's convenient if you have this requirement. However, Google expects to give users the most direct prompt in the simplest way, so this way is not recommended by Google, but it doesn't prevent us from customizing our own Toast to give users a different experience.