Disposición de ancho de porcentaje de Android
Necesito establecer el ancho de mi vista como 50% del ancho de la pantalla y luego centrar esta vista horizontalmente, mientras que potencialmente tener uno o más botones que pueden aparecer adjunto a la izquierda o el lado derecho de la pantalla.
Estoy utilizando un diseño relativo para que pueda colocar un diseño lineal con pesas para obtener mi 50% centrado al colocar cualquier botón en la parte superior de ese LL conectado al borde izquierdo o derecho de la RL. Sin embargo, este diseño falta la barra media azul. Si establezco el layout_weight de la vista media a 1 obtengo 3 barras de igual tamaño.
- Problema de diseño de Android: anchos relativos en porcentaje con el peso
- Archivos de diseño recién creados no agregados a R.java
- El selector no funciona con el diseño y la vista de imagen
- Crear componente personalizado basado en LinearLayout, declarando el diseño en XML
- Cómo obtener el tamaño de texto predeterminado en píxeles en Android
<RelativeLayout android:layout_width="match_parent" android:layout_height="48dp"> <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" /> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="2" /> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" /> </LinearLayout> </RelativeLayout>
- Centrar dos botones horizontalmente
- ¿Por qué no se permite inflar mi TableLayout?
- Cómo cambiar el icono de un botón mediante programación?
- Detectando OutOfMemoryError en la decodificación Bitmap
- Android include tag - referencia de diseño no válida
- Modificación del widget de barra de búsqueda de Android para que funcione verticalmente
- ¿Cómo mantener algunos de los componentes fijos cuando entra el teclado blando?
- Mutiple fragmentos en un vertical Linearlayout
Debe establecer el ancho de la vista a 0dip
<RelativeLayout android:layout_width="match_parent" android:layout_height="48dp"> <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" /> <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="2" /> <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" /> </LinearLayout> </RelativeLayout>
Puede lograr esto con la nueva biblioteca porcentual:
https://developer.android.com/tools/support-library/features.html#percent
Haciendo algo como esto:
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="48dp"> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#FF0000" app:layout_widthPercent="25%" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#0000FF" android:centerHorizontal="true" app:layout_marginLeftPercent="25%" app:layout_widthPercent="50%" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:alignParentRight="true" android:background="#00FF00" app:layout_marginLeftPercent="75%" app:layout_widthPercent="25%" /> </android.support.percent.PercentRelativeLayout>
Utilice una nueva biblioteca de soporte porcentual.
compile 'com.android.support:percent:24.0.0'
Ejemplo:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent"> <android.support.percent.PercentRelativeLayout android:layout_width="match_parent" android:layout_height="48dp" > <View android:layout_height="match_parent" android:layout_weight="1" android:background="#FF0000" app:layout_widthPercent="33%" /> <View android:layout_height="match_parent" android:layout_weight="2" android:background="#0000FF" app:layout_marginLeftPercent="33%" app:layout_widthPercent="33%" /> <View android:layout_height="match_parent" android:layout_weight="1" android:background="#00FF00" app:layout_marginLeftPercent="66%" app:layout_widthPercent="33%" /> </android.support.percent.PercentRelativeLayout> </LinearLayout>
Para más información Android Doc
* Para Muestra y Tutorial * Tutorial1 Tutorial2 Tutorial3
Si entiendo correctamente, necesitas esto:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" > <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" ></View> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="1.5" ></View> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1.5" ></View> </LinearLayout> </RelativeLayout>