Tratar de que Android LinearLayout se estire horizontal y verticalmente
ACTUALIZACIÓN: Aprecio el consejo de usar GridView, y creo que proporcionaría un mejor rendimiento. Sin embargo, mis esfuerzos iniciales en la implementación de un GridView simple han resultado en un fallo vergonzoso. Supongo que averiguar lo que estoy haciendo mal es por otro tema. Para corregir esta implementación, utilicé OnWindowFocusChanged para encontrar la altura de ANDROID_CONTENT y luego dividirla por el número de filas para establecer la altura de la fila. La lección principal aquí que no está presente en muchas de las discusiones sobre esta cuestión es OnCreate es el lugar equivocado para establecer las dimensiones. OnWindowFocusChanged se produce más tarde cuando se pueden realizar mediciones precisas. Esta información no se menciona a menudo en las muchas, muchas discusiones sobre este tema.
ORIGINAL: Estoy trabajando en un diseño de Android intentando configurar un tablero con tres columnas y cuatro filas.
- ¿Hay alguna implementación de StAX para Android?
- Combinar lienzo y diseño (Android)
- Android Flavors, ContentProviders, SyncAdapters y AccountManager
- Utilice símbolo especial (<,>) en diseño de diseño en Android
- ¿Puede explicar alguien inflar método? Mayor comprensión de las vistas de Android
Quiero estirar el tablero para llenar la pantalla disponible tanto vertical como horizontalmente. Traté de anidar LinearLayout por fila y columna con el weight="1"
opción, pero sólo podía conseguir que se estire horizontalmente.
En la siguiente configuración, las filas se extienden para llenar el ancho de la pantalla, pero las columnas se establecen en "60dp" . He intentado GridLayout y TableLayout también, pero no han sido capaces de obtener el resultado deseado.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/background" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft = "10dp" android:paddingRight = "10dp" android:background="@drawable/bg" > <LinearLayout android:id="@+id/player1" android:layout_width="match_parent" android:layout_height="@dimen/positionHeight" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/player2" android:layout_width="match_parent" android:layout_height="@dimen/positionHeight" android:layout_alignParentTop="true" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/row0" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/player2" android:layout_alignParentLeft="true" android:baselineAligned="false" android:orientation="horizontal"> <LinearLayout android:id="@+id/position0" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position1" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position2" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> </LinearLayout> <LinearLayout android:id="@+id/row1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/row0" android:layout_alignParentLeft="true" android:baselineAligned="false" android:orientation="horizontal"> <LinearLayout android:id="@+id/position3" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position4" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position5" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> </LinearLayout> <LinearLayout android:id="@+id/row2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/row1" android:layout_alignParentLeft="true" android:baselineAligned="false" android:orientation="horizontal"> <LinearLayout android:id="@+id/position6" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position7" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position8" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> </LinearLayout> <LinearLayout android:id="@+id/row3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/row2" android:layout_alignParentLeft="true" android:baselineAligned="false" android:orientation="horizontal"> <LinearLayout android:id="@+id/position9" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position10" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> <LinearLayout android:id="@+id/position11" android:layout_width="0dip" android:layout_weight="1" android:layout_height="@dimen/positionHeight" android:orientation="horizontal" android:background="@drawable/square" /> </LinearLayout>
por favor déme la sugerencia.
- AdView - Falta de adActivity con android: configChanges en AndroidManifest.xml
- El signo de interrogación me parece nuevo
- Error: (3) Error al analizar XML: no bien formado (token no válido)
- Balloon in Chatting Aplicación para Android
- Cómo cambiar el nombre de la aplicación en NativeScript
- Cómo quitar la barra de título en android?
- Cambiar imagen
- android.view.InflateException error inflar clase
No estoy de acuerdo con otros diciendo que debes usar un GridLayout. Pruebe este enfoque con LinearLayout. Utilicé una vista genérica para cada celda de la cuadrícula, pero cualquier control funcionará.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
Este enfoque también requiere ninguna manipulación manual del diseño en código, todo se maneja en el xml usando pesos.
Tienes que intentarlo.
<ImageView android:id="@+id/iv_clm1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/yellow" /> <ImageView android:id="@+id/iv_clm1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/red" /> </LinearLayout> <!-- second Row --> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:orientation="horizontal" > <ImageView android:id="@+id/iv_clm1" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/red" /> <ImageView android:id="@+id/iv_clm2" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/yellow" /> <ImageView android:id="@+id/iv_clm3" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/red" /> </LinearLayout>
En general, no es una buena idea usar tantos diseños. Debe utilizar TableLayout
o GridLayout
en GridLayout
lugar.
Diseño de cuadrícula:
- Preguntas y respuestas relacionadas con StackOverflow
- Tutorial oficial
- Muy buen tutorial
TableLayout:
- Tutorial oficial
Como mencioné en mi UPDATE anterior, el verdadero truco aquí es establecer las dimensiones en algún lugar que no sea OnCreate. He inicializado player1, player2 y position [] como los elementos LinearLayout del xml, luego en onWindowFocusChanged ajusté su altura para encajar. No es tan bueno como GridView como sugerido por otros aquí, pero es algo que realmente puedo hacer el trabajo!
con ayuda de:
http://www.rapidsnail.com/developer/topic/2011/222/27/35613/title-bar-and-get-out-of-the-view-of-the-status-bar-display-area- height.aspx
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); View content = getWindow().findViewById(Window.ID_ANDROID_CONTENT); player1.getLayoutParams().height=content.getHeight()/6; player2.getLayoutParams().height=content.getHeight()/6; for (int i=0;i<12;i++){ position[i].getLayoutParams().height=content.getHeight()/6; }