你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中的Toast

[日期:2011-12-19] 来源:Linux社区  作者:xinem [字体:大 中 小]
创建定制布局的Toast

首先要创建定制布局的Toast的layout:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:Android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_height="wrap_content" android:layout_width="wrap_content"  
  5.     android:background="#ffffffff" android:orientation="vertical"  
  6.     android:id="@+id/llToast" >  
  7.     <TextView  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_margin="1dip"  
  10.         android:textColor="#ffffffff"  
  11.         android:layout_width="fill_parent"  
  12.         android:gravity="center"  
  13.         android:background="#bb000000"  
  14.         android:id="@+id/tvTitleToast" />  
  15.     <LinearLayout  
  16.         android:layout_height="wrap_content"  
  17.         android:orientation="vertical"  
  18.         android:id="@+id/llToastContent"  
  19.         android:layout_marginLeft="1dip"  
  20.         android:layout_marginRight="1dip"  
  21.         android:layout_marginBottom="1dip"  
  22.         android:layout_width="wrap_content"  
  23.         android:padding="15dip"  
  24.         android:background="#44000000" >  
  25.         <ImageView  
  26.             android:layout_height="wrap_content"  
  27.             android:layout_gravity="center"  
  28.             android:layout_width="wrap_content"  
  29.             android:id="@+id/tvImageToast" />  
  30.         <TextView  
  31.             android:layout_height="wrap_content"  
  32.             android:paddingRight="10dip"  
  33.             android:paddingLeft="10dip"  
  34.             android:layout_width="wrap_content"  
  35.             android:gravity="center"  
  36.             android:textColor="#ff000000"  
  37.             android:id="@+id/tvTextToast" />  
  38.     </LinearLayout>  
  39. </LinearLayout>  

我们创建的这个Toast,有一个header,一个图像和一段message。现在我们需要把layout应用在我们创建的Toast上:

  1. LayoutInflater inflater = getLayoutInflater();  
  2. View layout = inflater.inflate(R.layout.customtoast,  
  3.    (ViewGroup) findViewById(R.id.llToast));  
  4. ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);  
  5. image.setImageResource(R.drawable.codeprojectlogo);  
  6. TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);  
  7. title.setText("Attention");  
  8. TextView text = (TextView) layout.findViewById(R.id.tvTextToast);  
  9. text.setText("Hello, The Code Project!");  
  10. Toast toast = new Toast(getApplicationContext());  
  11. toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);  
  12. toast.setDuration(Toast.LENGTH_LONG);  
  13. toast.setView(layout);  
  14. toast.show();  
toast4.pnglinux
相关资讯       Android开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款