¿Qué significa inflar una vista desde un archivo xml?


Soy nuevo en el desarrollo de Android y sigo encontrando referencias a inflar vistas desde un archivo xml de diseño. Busqué en Google y busqué en la guía de desarrollo, pero todavía no pude captar un sentido de lo que significa. Si alguien pudiera dar un ejemplo muy simple, sería muy apreciado.

Author: Brendan Weinstein, 2011-01-02

5 answers

Cuando escribes un diseño XML, será inflado por el sistema operativo Android, lo que básicamente significa que se renderizará creando view object en memoria. Llamemos a eso inflación implícita (el sistema operativo inflará la vista por usted). Por ejemplo:

class Name extends Activity{
    public void onCreate(){
         // the OS will inflate the your_layout.xml
         // file and use it for this activity
         setContentView(R.layout.your_layout);
    }
}

También puede inflar las vistas explícitamente utilizando LayoutInflater. En ese caso tienes que:

  1. Obtener una instancia de la LayoutInflater
  2. Especifique el XML a inflar
  3. Utilice el View

Por ejemplo:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
 233
Author: Cristian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-21 15:32:40

"Inflar" una vista significa tomar el XML de diseño y analizarlo para crear los objetos view y viewgroup a partir de los elementos y sus atributos especificados, y luego agregar la jerarquía de esas vistas y viewgroup al grupo de vista principal. Cuando llamas a setContentView (), adjunta las vistas que crea al leer el XML a la actividad. También puede usar LayoutInflater para agregar vistas a otro ViewGroup, que puede ser una herramienta útil en muchas circunstancias.

 145
Author: jjb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-02 17:06:23

Inflar es el proceso de agregar una vista(.xml) a la actividad en tiempo de ejecución. Cuando creamos un ListView inflamos su cada elemento dinámicamente. Si queremos crear un ViewGroup con múltiples vistas como botones y textview . Podemos crearlo como

Button but = new Button();
but.setText ="button text";
but.background ...
but.leftDrawable.. and so on...

TextView txt = new TextView();
txt.setText ="button text";
txt.background ... and so on...

Luego tenemos que crear un layout donde podamos añadir las vistas anteriores

RelativeLayout rel = new RelativeLayout();

rel.addView(but);

Y ahora si queremos añadir botón en la esquina derecha y textview en la parte inferior . Tenemos que hacer mucho trabajo. Primero con las propiedades de la vista y en segundo lugar, tenemos que aplicar múltiples restricciones. Es un trabajo duro y lento.

Android que sea fácil para nosotros para crear un simple .xml y diseñar su estilo y atributos en xml y simplemente inflarlo donde lo necesitemos sin la confusión de establecer restricciones y configurarlo programáticamente.

LayoutInflater inflater = 
              (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like

(RelativeLayout)findViewById(R.id.relative).addView(menuLayout);
 28
Author: Xar E Ahmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-05 10:45:59

Creo que aquí "inflar una vista" significa obtener el diseño.archivo xml dibujar una vista especificada en ese archivo xml y RELLENAR ( = inflar ) el grupo de vista principal con la vista creada.

 4
Author: Jaydeep Ranipa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-21 11:54:44

Debido a que hacemos UI en XML pero view objects es lo que mostramos, por lo que de alguna manera necesitamos convertir xml en view objects, por lo que inflar significa que estamos convirtiendo xml en view objects para que se pueda mostrar, para esto necesitamos un servicio llamado layout inflator service y darle un xml y se convertirá para usted.

 1
Author: blackHawk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-21 05:54:31