默认情况下是在屏幕的中央进行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:orientation="vertical" >
<Button
android:id="@+id/but"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义风格的Toast显示" />
</LinearLayout>
在MyToastDemo.java程序中
package com.tarena.toast;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MyToastDemo extends Activity {
private Button but = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.but = (Button) super.findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerImpl()) ;
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
Toast myToast = Toast.makeText(MyToastDemo.this, "自定义风格的Toast信息提示框",
Toast.LENGTH_LONG);
myToast.setGravity(Gravity.CENTER, 60, 30);
LinearLayout myToastView = (LinearLayout)myToast.getView(); //线性布局
ImageView img = new ImageView(MyToastDemo.this);
img.setImageResource(R.drawable.pt);
myToastView.addView(img,0); //图片放在最前面
myToast.show();
}
}
}