Android:自定义吐司中的ViewGroup



我在应用程序中创建了一个自定义Toast,然后我放了一个从任何地方调用它。它需要findViewById中的ViewGroup。但是我不知道当我上了扩展Activity且不是事件的课程时发送它...谢谢

Module.java

public static void toast(Context context, View view, String texto, int perfilColor, int icono) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View layouttoast = inflater.inflate(R.layout.toast, (ViewGroup)view.findViewById(R.id.toastcustom));
    ((TextView) layouttoast.findViewById(R.id.texttoast)).setText(Html.fromHtml(texto));
    ((ImageView) layouttoast.findViewById(R.id.imagenToast)).setImageResource(icono);
    ((LinearLayout) layouttoast.findViewById(R.id.toastcustom)).setBackgroundResource(cargarColorToast(perfilColor));
    Toast mytoast = new Toast(context);
    mytoast.setView(layouttoast);
    mytoast.setDuration(Toast.LENGTH_SHORT);
    mytoast.show();
}

mainActivity.java

...
if(...) {
    Module.toast(MainActivity.this, ¿? , "error", color, icon);
}

这就是我使用自定义吐司的方式。

public class CustomToast {
    public static Toast makeToast(Activity activity, String text, int duraion ){
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_achievement, (ViewGroup) activity.findViewById(R.id.toast_layout_root));
        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);
        Toast toast = new Toast(activity.getApplicationContext());
        toast.setDuration(duraion);
        toast.setView(layout);
        toast.setGravity(Gravity.CENTER, 0, 0);
        return toast;
    }
}

和xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_margin="10dp"
    android:padding="8dp" >
 <!-- content goes here-->
</LinearLayout>

不是ID必须是toast_layout_root或它不起作用。

基本上,您无需随时随地传递视图。

最新更新