Android Studio cómo hacer una columna de 2 LinearLayout
Soy muy nuevo en Android App Development, y estoy tratando de lograr el siguiente diseño de los botones en Android Studio.
[
- ¿Por qué usamos xml?
- Contenido detrás de CoordinatorLayout AppBarLayout
- No se puede iniciar la actividad ComponentInfo {com.example.countryselect / com.example.countryselect.OfferSelect}
- Android - Personalizar el cuadro de diálogo con un diseño Xml
- Android: validar el archivo xml en tiempo de compilación?
He estado intentando usar un diseño lineal, pero no pude hacerlo bien.
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:weightSum="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:background="#016eff" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_margin="10dp" android:textColor="#ffffff" android:layout_weight="0.48" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:background="#016eff" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_margin="10dp" android:textColor="#ffffff" android:layout_weight="0.48" /> </LinearLayout>
El problema con esto es, si agregué otro botón a la disposición lineal, entonces apenas consiguen aplastados juntos, en vez de agregar el botón a la fila siguiente.
¿Puede alguien por favor mostrarme para hacer mi LinearLayout sólo tiene 2 widgets en cada fila, o proporcionar otra solución.
Cualquier ayuda será muy apreciada gracias 🙂
- Obtener linearLayout desde otro xml Android
- ¿Cómo poner el carácter del espacio en un nombre de la secuencia en XML?
- Cómo configurar la transparencia de una imagen de fondo Android xml file
- Recurso de cadena duplicado en strings.xml android
- Ajuste EditText al tamaño de fuente
- Cómo definir el diseño en un PopupWindow de un archivo xml, cuando el método PopupWindow se llama desde una clase independiente
- Implementación de múltiples fragmentos en una sola actividad Dinámicamente
- Declaración de un elemento personalizado de la interfaz de usuario de Android con XML
LinearLayout está bien para lo que está tratando de lograr. Observe los atributos de peso y orientación de un objeto LinearLayout. Diseño Lineal
Y lo que quieres, puedes hacer por ejemplo así:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <TextView android:text="Whatever You Want Here" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="36sp"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <Button android:text="Button 1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> <Button android:text="Button 2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <Button android:text="Button 3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> <Button android:text="Button 4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <Button android:text="Button 5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> <Button android:text="Button 6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center"> <Button android:text="Button 7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> <Button android:text="Button 8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center"/> </LinearLayout> </LinearLayout> </LinearLayout>
Salida:
Y cuidado, porque anidar demasiados atributos de peso puede tener algunos problemas de rendimiento negativo.
Muy bien chicos, me las arreglé para encontrar una solución gracias al comentario de Vishwa. Sin embargo, en realidad no encontrar una forma de hacer un LinearLayout tienen 2 columnas.
En su lugar, cambié a un TableLayout y streched las columnas 0 & 1 para tomar toda la pantalla. Heres cómo mi XML terminó buscando. (Tiene cosas extra en él para obtener mi diseño)
<TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:stretchColumns="0,1"> <TableRow android:layout_width="match_parent" android:paddingBottom="8dp" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Events" android:id="@+id/eventButton" android:layout_column="0" android:background="#016eff" android:layout_marginRight="8dp" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Absentee" android:id="@+id/absenteeButton" android:layout_column="1" android:background="#016eff" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> </TableRow> <TableRow android:layout_width="match_parent" android:paddingBottom="8dp" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Contacts" android:id="@+id/contactsButton" android:layout_column="0" android:background="#016eff" android:layout_marginRight="8dp" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Alerts" android:id="@+id/alertButton" android:layout_column="1" android:background="#016eff" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> </TableRow> <TableRow android:layout_width="match_parent" android:paddingBottom="8dp" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Links" android:id="@+id/linksButton" android:layout_column="0" android:background="#016eff" android:layout_marginRight="8dp" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Newsletter" android:id="@+id/newsletterButton" android:layout_column="1" android:background="#016eff" android:textColor="#ffffff" android:textSize="40px" android:textStyle="normal" /> </TableRow> <TableRow android:layout_width="match_parent" android:paddingBottom="8dp" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Kamar" android:id="@+id/kamarButton" android:layout_column="0" android:background="#016eff" android:layout_marginRight="8dp" android:textColor="#ffffff" android:textStyle="normal" android:textSize="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="News" android:id="@+id/newsButton" android:layout_column="1" android:background="#016eff" android:textColor="#ffffff" android:textSize="40px" android:textStyle="normal" /> </TableRow> </TableLayout>
Usted acaba de poner un LinearLayout separado con android:orientation="horizontal"
alrededor de cada par de botones. Entonces el Parent LinearLayaout debería tener android:orientation="vertical"
y el weightsum debería estar en cada LinearLayout horizontal.
- Córdoba: ¿Cómo establecer diferentes nombres de paquetes por plataforma usando el cordova-cli?
- ¿El nombre de la actividad principal debe ser .MainActivity?