設置Toast顯示字體大小顏色
- LayoutInflater inflater = LayoutInflater
- .from(getApplicationContext());
- View view = inflater.inflate(R.layout.my_toast,
- (ViewGroup) findViewById(R.id.toast_layout_root));
- TextView textView = (TextView) view.findViewById(R.id.text);
- SpannableString ss = new SpannableString("掃描成功");
- ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 4,
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- textView.setText(ss);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(view);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
- mytoast.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toast_layout_root"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- android:padding="10dp" >
- <TextView
- android:layout_marginTop="800dip"
- android:textSize="25dip"
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="50dip"
- android:textColor="@color/black" />
- </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();