Wywołanie getLayoutInflater() w miejscach nieaktywnych
Co trzeba zaimportować lub jak nazwać Nadmuchiwacz układu w miejscach innych niż aktywność?
public static void method(Context context){
//this doesn't work the getLayoutInflater method could not be found
LayoutInflater inflater = getLayoutInflater();
// this also doesn't work
LayoutInflater inflater = context.getLayoutInflater();
}
Jestem w stanie wywołać getLayoutInflater
tylko w aktywności, czy to ograniczenie? Co jeśli chcę utworzyć niestandardowe okno dialogowe i chcę nadmuchać widok dla niego, lub co jeśli chcę mieć wiadomość Toast z niestandardowym widokiem, który jest wyświetlany z usługi, mam tylko kontekst z usługi nie mam żadnej aktywności, ale chcę pokazać niestandardową wiadomość.
Potrzebuję pompki w miejscach w kodzie, którego nie ma w klasie activity.
Jak mogę to zrobić ?4 answers
Możesz skorzystać z tych działań zewnętrznych - wystarczy, że podasz Context
:
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Następnie, aby pobrać różne widżety, nadmuchujesz układ:
View view = inflater.inflate( R.layout.myNewInflatedLayout, null );
Button myButton = (Button) view.findViewById( R.id.myButton );
Edycja na Lipiec 2014
Odpowiedź Davide ' a na temat tego, jak uzyskać LayoutInflater
jest właściwie bardziej poprawna niż moja(która jest nadal ważna).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:34:44
Lub ...
LayoutInflater inflater = LayoutInflater.from(context);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-22 10:21:40
Za pomocą obiektu context można uzyskać LayoutInflater z następującego kodu
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-18 07:31:31
Lub
View.inflate(context, layout, parent)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-10 21:50:46