本文实例讲述了Android实现Toast提示框图文并存的方法。分享给大家供大家参考,具体如下:
程序如下:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.util.Linkify;
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.TextView;
import android.widget.Toast;
public class A05Activity extends Activity {
private Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button);
b.setText("显示Toast");
b.setBackgroundColor(Color.GREEN);
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView iv=new ImageView(A05Activity.this);
iv.setImageResource(R.drawable.icon);
TextView tv=new TextView(A05Activity.this);
tv.setText(R.string.title);
LinearLayout ll=new LinearLayout(A05Activity.this);
//判断TextView中的内容是什么格式,并与系统连接
Linkify.addLinks(tv, Linkify.PHONE_NUMBERS|Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS);
Toast t=Toast.makeText(A05Activity.this, tv.getText(), Toast.LENGTH_LONG);
View v1=t.getView();
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(iv);
ll.addView(v1);
t.setView(ll);
//设置Toast对象在手机中的相对位置
t.setGravity(Gravity.CENTER, 50, 50);
t.show();
}
});
}
}
注意:因为Toast属于Activiyt里的Context,所以在Toast里面的连接是无法单击的。比如在Toast中存在网址等内容是无法在Toast里面双击打开的。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》及《Android开发入门与进阶教程》
希望本文所述对大家Android程序设计有所帮助。