Customize a Toast)

Source: Internet
Author: User

Customize a Toast

To achieve this effect:

 

 

Use the following code:

LayoutInflater inflater = LayoutInflater. from (this );
View view = inflater. inflate (R. layout. book_reading_seekbar_toast, null );
TextView chapterNameTV = (TextView) view. findViewById (R. id. chapterName );
TextView percentageTV = (TextView) view. findViewById (R. id. percentage );
ChapterNameTV. setText (chapterName );
PercentageTV. setText (df. format (persent * 100) + "% ");

Toast toast = new Toast (this );
Toast. setGravity (Gravity. BOTTOM, 0, PixelFormat. formatDipToPx (this, 70 ));
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (view );
Toast. show ();

This effect can be achieved, but it is not enough! With the help of layout:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ null">

<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "24dp"
Android: layout_marginRight = "24dp"
Android: background = "@ drawable/book_reading_toast_bg"
Android: gravity = "center"
Android: orientation = "vertical"
Android: paddingBottom = "37dp"
Android: paddingTop = "39dp">

<TextView
Android: id = "@ + id/chapterName"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: ellipsize = "end"
Android: gravity = "center"
Android: singleLine = "true"
Android: text = "800th carp jump Longmen"
Android: textColor = "# ffffff"
Android: textSize = "20sp"/>

<TextView
Android: id = "@ + id/percentage"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/chapterName"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "8dp"
Android: gravity = "center"
Android: text = "45%"
Android: textColor = "# ffffff"
Android: textSize = "16sp"/>
</RelativeLayout>

</LinearLayout>
Use LInearLayout to open the layout and adjust the layout parameters with RelativeLayout! Hit the final result!


However, there is still a problem: although the effect can be the same, sometimes it is easier to set some things in the code! Therefore, use the following code:

PixelFormat. formatDipToPx (this, 70)

This function is to convert the 70dp in the cut graph of the artist into the value of the pix. In this way, the same effect can be displayed on different screens to adapt to all screens!
I will also send this conversion method:

Import android. app. Activity;
Import android. content. Context;
Import android. util. DisplayMetrics;

/**
* Pixel Conversion

*/
Public class PixelFormat {
/**
* Convert dip units to px units
*
* @ Param context
* Context object
* @ Param dip
* Dip Value
* @ Return
*/
Public static int formatDipToPx (Context context, int dip ){
DisplayMetrics dm = new DisplayMetrics ();
(Activity) context). getWindowManager (). getdefadisplay display ()
. GetMetrics (dm );
Return (int) Math. ceil (dip * dm. density );
}

/**
* Convert px Unit to dip Unit
*
* @ Param context
* Context object
* @ Param px
* Px Value
* @ Return
*/
Public static int formatPxToDip (Context context, int px ){
DisplayMetrics dm = new DisplayMetrics ();
(Activity) context). getWindowManager (). getdefadisplay display ()
. GetMetrics (dm );
Return (int) Math. ceil (px * 160)/dm. densityDpi ));
} Www.2cto.com

}
I forgot to add the rounded corner:
Book_reading_toast_bg. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Shape xmlns: android = "http://schemas.android.com/apk/res/android">

<Solid android: color = "# aa000000"/>

<Corners android: radius = "10dp"/>

<Padding
Android: bottom = "5dp"
Android: left = "5dp"
Android: right = "5dp"
Android: top = "5dp"/>

</Shape>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.