自定义吐司以及Toast的各种用法,详解。教你学会使用各种各样的Toast
package com.david_Custoast; import com.david_Custoast.R; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 默认吐司 public void showNormalToast(View view) { // Toast.makeText(this, "我显示了", Toast.LENGTH_SHORT).show(); Toast toast = Toast.makeText(this, "我显示了", Toast.LENGTH_SHORT); toast.show(); } // 其他对齐方式的吐司 public void showGravityToast(View view) { Toast toast = Toast.makeText(this, "我又显示了", Toast.LENGTH_SHORT); // 设置吐司的展示位置 toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } // 自定义吐司 public void showCustomToast(View view) { // 获取吐司的根视图 View rootView = LayoutInflater.from(this).inflate(R.layout.toast_custom, null); ImageView iv = (ImageView) rootView.findViewById(R.id.iv); TextView tv = (TextView) rootView.findViewById(R.id.tv); iv.setImageResource(R.drawable.f029); tv.setText("么么哒"); Toast toast = new Toast(this); // 修改吐司对齐方式 toast.setGravity(Gravity.CENTER, 0, 0); // 设置跟视图 toast.setView(rootView); // 设置展示时间 toast.setDuration(Toast.LENGTH_LONG); // 展示吐司 toast.show(); } }
源代码下载地址:http://download.csdn.net/detail/qq_21376985/9468483
程序员小冰博客:http://blog.csdn.net/qq_21376985
技术交流QQ986945193
微博:http://weibo.com/mcxiaobing