2009-12-21 56 views
0

我想创建一个帮助器类来显示我的android应用程序中的Toasts,如下所示。android吐司和查看帮助

公共类ToastHelper {

final static Toast toastAlert(String msg) { 
    LayoutInflater inflater = LayoutInflater.from(Main.self.getApplicationContext()); 
    View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); 

    TextView tv = (TextView)layout.findViewById(R.id.toast_text); 
    tv.setText(msg); 

    Toast toast = new Toast(Main.self.getApplicationContext()); 
    toast.setDuration(Toast.LENGTH_SHORT); 
    toast.setView(layout); 
    return toast; 
} 

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/toast_layout_root" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dip" 
      android:background="#FFF" 
      > 
<TextView android:id="@+id/toast_text" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:textColor="#000" 
      /> 

我的代码是基于这个例子:http://developer.android.com/guide/topics/ui/notifiers/toasts.html

我的问题是,当我尝试膨胀该视图,我不能从acti的视图调用findViewById vity我打电话给toastAlert英寸有没有一种方法可以访问该视图?

回答

1

我唯一的想法是将一个View传递给该方法。它不会那么糟糕,全部是这样的:

class.toastAlert(this, "My Toast"); 
+0

我记得很多帮助函数最终需要一个视图或上下文,最简单的处理方法就是传递它。 – 2009-12-21 21:16:33

0

我用这个来获得LayoutInflator

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 

我不知道这是否会帮助或没有。