简单自定义Toast

xiaoxiao2021-02-28  98

准备材料: 自定义Toast的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_people" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是自定义吐司" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> 开始使用: 从源码中直接复制稍作修改,稍作修改View v = inflate.inflate(R.layout.MyToast, null); TextView tv = (TextView) v.findViewById(R.id.textView1); public static void MyToast(Context context, CharSequence text, int i) { Toast result = new Toast(context); LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflate.inflate(R.layout.MyToast, null); TextView tv = (TextView) v.findViewById(R.id.textView1); tv.setText(text); result.setView(v); result.setDuration(i); result.show(); }
转载请注明原文地址: https://www.6miu.com/read-97326.html

最新回复(0)