自定义Toast
扫描二维码
随时随地手机看文章
package com.example.administrator.layoutoptimize; import android.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewStub; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private boolean inflated = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ViewStub viewStub = (ViewStub) findViewById(R.id.viewStub); if (!inflated) { inflated = true; viewStub.inflate(); } } }); Toast toast = Toast.makeText(this, "带图片", Toast.LENGTH_LONG); ImageView img = new ImageView(this); img.setImageResource(R.drawable.img); /** * 只有图片 */ // toast.setView(img); // toast.setGravity(Gravity.CENTER,0,0); // toast.show(); /** * 图片加文字 */ LinearLayout linearLayout = (LinearLayout)toast.getView(); linearLayout.addView(img); //设置index 0 图片在上 toast.setView(linearLayout); toast.show(); /** * 自定义 */ View view = View.inflate(this,R.layout.toast,null); toast.setView(view); toast.show(); } } ![这里写图片描述](http://img.blog.csdn.net/20150723174825189)