烤面包一般用来显示一行文字,用法比较固定:
吐司。makeText(上下文语境,字符串消息,int持续时间); >之前但是有时候想用面包来显示复杂的观点甚至是带有图片的观点时这时候就要用到自定义的吐司,自定义吐司主要用到一下几个方法如图:
<强> Toast_view。xml:
强>& lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=" match_parent " android:取向=按怪薄? android:重力="中心" 比; & lt; ImageView android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: src=" https://www.yisu.com/zixun/@drawable tianjia_p " android: layout_gravity="中心"/比; & lt; TextView android: layout_width=" wrap_content " android: layout_height=" wrap_content " android:文本=" hello world " android: layout_gravity=" center_horizontal " android: textSize=" 20 dp”/比; & lt;/LinearLayout><强> Activity_main.xml: 强>
& lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android " android: layout_width=" match_parent " android: layout_height=" match_parent " 比; & lt;按钮 android: layout_width=" wrap_content " android: layout_height=" wrap_content " android: id=癅 + id/色” android:文本="按钮"/比; & lt;/LinearLayout><强> ToastUtil: 强>
公开课ToastUtil { 私有静态土司; 公共静态孔隙showToast (int持续时间、视图mview,上下文语境) { 如果(烤面包==null) { 烤面包=new吐司(上下文); } toast.setDuration(持续时间); toast.setView (mview); toast.show (); } } >之前<强> MainActivity: 强>
公开课MainActivity延伸AppCompatActivity { 私人观点toast_view; 私人按钮按钮; @Override 保护无效onCreate(包savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); LayoutInflater增压泵=LayoutInflater.from(这个); toast_view=inflater.inflate (R.layout.toast_view, null); 按钮=(按钮)findViewById (R.id.myButton); 按钮。setOnClickListener(新View.OnClickListener () { @Override 公共空间onClick(查看v) { ToastUtil.showToast (Toast.LENGTH_SHORT toast_view getApplicationContext ()); } }); } }上面的ToastUtil工具类写的不好,大家可以自己改写自己的ToastUtil类。
以上这篇基于自定义面包全面解析就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
基于自定义面包全面解析