
Toast大家都很熟,不多说。直接上图上代码。
具体代码如下:
main.xml:
<?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:gravity="center" androID:orIEntation="vertical" androID:padding="5dip" > <button androID:ID="@+ID/btnSimpletoast" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="默认" > </button> <button androID:ID="@+ID/btnSimpletoastWithCustomposition" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="自定义显示位置" > </button> <button androID:ID="@+ID/btnSimpletoastWithImage" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="带图片" > </button> <button androID:ID="@+ID/btnCustomToast" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="完全自定义" > </button> <button androID:ID="@+ID/btnRunToastFromOtherThread" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="其他线程" > </button></linearLayout>
custom.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/llToast" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:background="#ffffffff" androID:orIEntation="vertical" > <TextVIEw androID:ID="@+ID/tvTitleToast" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_margin="1dip" androID:background="#bb000000" androID:gravity="center" androID:textcolor="#ffffffff" /> <linearLayout androID:ID="@+ID/llToastContent" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginBottom="1dip" androID:layout_marginleft="1dip" androID:layout_marginRight="1dip" androID:background="#44000000" androID:orIEntation="vertical" androID:padding="15dip" > <ImageVIEw androID:ID="@+ID/tvImagetoast" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" /> <TextVIEw androID:ID="@+ID/tvTextToast" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:gravity="center" androID:paddingleft="10dip" androID:paddingRight="10dip" androID:textcolor="#ff000000" /> </linearLayout></linearLayout>
package com.example.test;import androID.app.Activity;import androID.app.Actionbar;import androID.app.Fragment;import androID.content.Intent;import androID.os.Bundle;import androID.os.Handler;import androID.vIEw.Gravity;import androID.vIEw.LayoutInflater;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEwGroup;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.TextVIEw;import androID.Widget.Toast;import androID.os.Build;public class MainActivity extends Activity implements OnClickListener { Handler handler = new Handler(); @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); findVIEwByID(R.ID.btnSimpletoast).setonClickListener(this); findVIEwByID(R.ID.btnSimpletoastWithCustomposition).setonClickListener( this); findVIEwByID(R.ID.btnSimpletoastWithImage).setonClickListener(this); findVIEwByID(R.ID.btnCustomToast).setonClickListener(this); findVIEwByID(R.ID.btnRunToastFromOtherThread).setonClickListener(this); } public voID showToast() { handler.post(new Runnable() { @OverrIDe public voID run() { Toast.makeText(getApplicationContext(),"我来自其他线程!",Toast.LENGTH_SHORT).show(); } }); } @OverrIDe public voID onClick(VIEw v) { Toast toast = null; switch (v.getID()) { case R.ID.btnSimpletoast: Toast.makeText(getApplicationContext(),"默认Toast样式",Toast.LENGTH_SHORT).show(); break; case R.ID.btnSimpletoastWithCustomposition: toast = Toast.makeText(getApplicationContext(),"自定义位置Toast",Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER,0); toast.show(); break; case R.ID.btnSimpletoastWithImage: toast = Toast.makeText(getApplicationContext(),"带图片的Toast",0); linearLayout toastVIEw = (linearLayout) toast.getVIEw(); ImageVIEw imageCodeProject = new ImageVIEw(getApplicationContext()); imageCodeProject.setimageResource(R.drawable.ic_launcher); toastVIEw.addVIEw(imageCodeProject,0); toast.show(); break; case R.ID.btnCustomToast: LayoutInflater inflater = getLayoutInflater(); VIEw layout = inflater.inflate(R.layout.custom,(VIEwGroup) findVIEwByID(R.ID.llToast)); ImageVIEw image = (ImageVIEw) layout .findVIEwByID(R.ID.tvImagetoast); image.setimageResource(R.drawable.ic_launcher); TextVIEw Title = (TextVIEw) layout.findVIEwByID(R.ID.tvTitleToast); Title.setText("Attention"); TextVIEw text = (TextVIEw) layout.findVIEwByID(R.ID.tvTextToast); text.setText("完全自定义Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.top,12,40); toast.setDuration(Toast.LENGTH_LONG); toast.setVIEw(layout); toast.show(); break; case R.ID.btnRunToastFromOtherThread: new Thread(new Runnable() { public voID run() { showToast(); } }).start(); break; } }}运行即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android Toast的用法总结(五种用法)全部内容,希望文章能够帮你解决Android Toast的用法总结(五种用法)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)