このコードを使ってカスタムトーストを作ることができました
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup)findViewById(R.id.custom_toast));
TextView text = (TextView) layout.findViewById(R.id.toast_tv);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
しかし、目的がわからないのでLayoutInflater
、コードをこれに変更しました...
Toast toast = new Toast(getApplicationContext());
toast.setView(findViewById(R.id.custom_toast));
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
そして、「setViewが呼び出されたに違いない」というRuntimeExceptionが発生します。
使用せずにビューをトーストに割り当てることができないのはなぜ
LayoutInflater
ですか?LayoutInflater
このエクスペリエンスを他のカスタムビューに適用できるようにするための一般的な目的は何ですか?
編集:onListItemClick()
コンテンツが設定された後、インターフェイスメソッド
でこれらのコードを使用しています。