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
- <? XML version= "1.0" encoding="Utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="Horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:src="@drawable/smile_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:text="Welcome back"
- android:textsize="30px"
- android:textcolor="#00ff00"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </linearlayout>
Java code:
[Java]View Plaincopy
- Package android.li;
[Java]View Plaincopy
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.Gravity;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.Toast;
[Java]View Plaincopy
- Public class Ntoast extends Activity
- {
- Button Mbutton;
- @Override
- public void OnCreate (Bundle savedinstancestate)
- {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Mbutton = (Button) Findviewbyid (R.ID.BTN);
- Mbutton.setonclicklistener (new Onclicklistener ()
- {
- @Override
- public void OnClick (View v)
- {
- Toast toast_1 = Toast.maketext (ntoast. This, "", Toast.length_long);
- Toast_1.setgravity (Gravity.top, 0, 30);
- View v_1 = Ntoast. this.getlayoutinflater (). Inflate (r.layout.toast_1, null);
- Toast_1.setview (v_1);
- Toast_1.show ();
- }
- });
- }
- }
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!!!