Toast用法

来源:互联网 发布:数据库冗余 anomalies 编辑:程序博客网 时间:2024/05/16 05:37

设置Toast显示字体大小颜色

  1. LayoutInflater inflater = LayoutInflater  
  2.         .from(getApplicationContext());  
  3. View view = inflater.inflate(R.layout.my_toast,  
  4.         (ViewGroup) findViewById(R.id.toast_layout_root));  
  5. TextView textView = (TextView) view.findViewById(R.id.text);  
  6.   
  7. SpannableString ss = new SpannableString("扫描成功");  
  8.   
  9. ss.setSpan(new ForegroundColorSpan(Color.RED), 04,  
  10.         Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  11. textView.setText(ss);  
  12.   
  13. toast.setDuration(Toast.LENGTH_LONG);  
  14. toast.setView(view);  
  15. toast.setGravity(Gravity.CENTER, 00);  
  16. toast.show();  
Java代码  收藏代码
  1.  mytoast.xml  
Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/toast_layout_root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="horizontal"  
  7.     android:padding="10dp" >  
  8.   
  9.     <TextView  
  10.         android:layout_marginTop="800dip"  
  11.         android:textSize="25dip"  
  12.         android:id="@+id/text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="fill_parent"  
  15.         android:layout_marginLeft="50dip"  
  16.           
  17.         android:textColor="@color/black" />  
  18.   
  19. </LinearLayout> 

设置Toast显示位置
 两个方法可以设置显示位置:
 方法一:setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移,垂直向下位移)
 方法二:setMargin(float horizontalMargin, float verticalMargin)

 以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)

 

Toast toast = Toast.makeText(this,"最高记录:" + pre.getLong("total", 0), Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP , 0, 50);
toast.show();

0 0
原创粉丝点击