这个是我做的,虽然比较难看能说明问题就行。
一下是自定义toast代码:
package com.rytong.toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
MyToast.myTosat(MainActivity.this, R.drawable.icon, "显示图片", Toast.LENGTH_LONG);
}
});
}
}
package com.rytong.toast;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MyToast {
public static void myTosat(Context context , int imageId ,String content , int duration){
//new一个toast传入要显示的activity的上下文
Toast toast = new Toast(context);
//显示的时间
toast.setDuration(duration);
//显示的位置
toast.setGravity(Gravity.BOTTOM, 0, 300);
//重新给toast进行布局
LinearLayout toastLayout = new LinearLayout(context);
toastLayout.setOrientation(LinearLayout.HORIZONTAL);
toastLayout.setGravity(Gravity.CENTER_VERTICAL);
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageId);
//把imageView添加到toastLayout的布局当中
toastLayout.addView(imageView);
TextView textView = new TextView(context);
textView.setText(content);
// textView.setBackgroundColor(Color.GRAY);
//把textView添加到toastLayout的布局当中
toastLayout.addView(textView);
toastLayout.setBackgroundColor(Color.GRAY);
//把toastLayout添加到toast的布局当中
toast.setView(toastLayout);
toast.show();
}
}