Customize your toast and want it to show anything line!!!

Source: Internet
Author: User

Tags: androidlayoutbuttonencodingxmlclass

Today talk about a common small control--toast. The general usage does not say, say a new usage, let toast inside fully display your custom view. Nonsense not much to say, start on the code!

The first is the view XML file: @drawable/smile_1, this picture casually find one, put in res/drawable-hdpi can, not too big, otherwise not beautiful.

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="Horizontal"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:src="@drawable/smile_1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"/>
  11. <TextView
  12. android:text="Welcome back"
  13. android:textsize="30px"
  14. android:textcolor="#00ff00"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"/>
  17. </linearlayout>


Java code:

[Java]View Plaincopy
    1. Package android.li;
[Java]View Plaincopy
    1. Import android.app.Activity;
    2. Import Android.os.Bundle;
    3. Import android.view.Gravity;
    4. Import Android.view.View;
    5. Import Android.view.View.OnClickListener;
    6. Import Android.widget.Button;
    7. Import Android.widget.Toast;
[Java]View Plaincopy
  1. Public class Ntoast extends Activity
  2. {
  3. Button Mbutton;
  4. @Override
  5. public void OnCreate (Bundle savedinstancestate)
  6. {
  7. super.oncreate (savedinstancestate);
  8. Setcontentview (R.layout.main);
  9. Mbutton = (Button) Findviewbyid (R.ID.BTN);
  10. Mbutton.setonclicklistener (new Onclicklistener ()
  11. {
  12. @Override
  13. public void OnClick (View v)
  14. {
  15. Toast toast_1 = Toast.maketext (ntoast.   This, "", Toast.length_long);
  16. Toast_1.setgravity (Gravity.top, 0, 30);
  17. View v_1 = Ntoast.  this.getlayoutinflater (). Inflate (r.layout.toast_1, null);
  18. Toast_1.setview (v_1);
  19. Toast_1.show ();
  20. }
  21. });
  22. }
  23. }


Finally look at the effect: do not feel like toast it! But he is! You can modify the display effect in the XML above, and add other components, whatever you want. Personally feel too much is not good, after all, just a small component, not too big!

Entire Directory structure:

Complete!

Customize your toast and want it to show anything line!!!

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.