jueves, 29 de octubre de 2015

Explicación de wrap_content, fill_parent o match_parent en Android Studio

   


En androide cada etiqueta en el archivo XML contiene dos atributos importantes:

  • android: layout_width
  • android: layout_height
Este atributo puede llegar a toma el valor wrap_content o fill_parent y este valor especificar la altura o la anchura de la vista solicitada. 

Para entenderlos mejor, veamos sus definiciones:
  1. wrap_content: Este componente se muestra lo suficientemente grande como para incluir su contenido solamente.
  2. fill_parent: El componente se muestra tan grande como su padre, y rellenar los espacios restantes. (match_parent renombrado en el Nivel API 8). 
Vamos a ver unos cuantos ejemplo para entenderlos mejor:

Usando en el botón:


 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  

Modificamos el archivo activity_main.xml de android studio y creamos un botón básico dentro LinearLayout y cambiar los valores de los atributos de android: layout_width y android: layout_height: 

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <Button  
     android:id="@+id/boton_prueba"  
     android:text="Boton Prueba"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  


Usando en el botón:
android:layout_width="match_parent"
android:layout_height="wrap_content"
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <Button  
     android:id="@+id/boton_prueba"  
     android:text="Boton Prueba"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  


Usando en el botón:
        android:layout_width="wrap_content"
        android:layout_height="match_parent"

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <Button  
     android:id="@+id/boton_prueba"  
     android:text="Boton Prueba"  
     android:layout_width="wrap_content"  
     android:layout_height="match_parent" />  
 </LinearLayout>  



Y por ultimo Usando en el botón: match_parent y match_parent
        android:layout_width="match_parent"
        android:layout_height="match_parent"


 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <Button  
     android:id="@+id/boton_prueba"  
     android:text="Boton Prueba"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent" />  
 </LinearLayout>  


Cesar GI

About Cesar GI

Lo que me importa es poder enseñar lo poco que se, por que asi como yo aprendo leyendo gracias a las personas que comparten sus conocimiento yo tambien quiero ayudar a la comunidad en español aportando lo poco que he aprendido hasta el momento.

7 comentarios

Write comentarios
Unknown
AUTHOR
23 de diciembre de 2016, 10:02 delete

Excelente, lo que buscaba saber!

Reply
avatar
Unknown
AUTHOR
9 de agosto de 2017, 10:48 delete

Buenas, si un tecto me soprepasa el wrap content como hago para que se amplie a una segunda linea?

Reply
avatar
Gonza
AUTHOR
28 de noviembre de 2017, 20:56 delete

Gracias, ni en la pag oficial de Android esta tan entendible

Reply
avatar