fije el ancho y la altura como porcentaje en el diseño relativo

Me gustaría tener esto: un ViewPager superpuesto por 2 flechas

introduzca la descripción de la imagen aquí

Y obtengo lo que quiero por este código. Sin embargo, recibí estas advertencias de pelusas:

  • esta disposición es inútil (para las disposiciones falsas)
  • los pesos anidados son malos para el rendimiento.

Me pregunto si hay una mejor manera de obtener esta interfaz de usuario como yo quiero.

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/dialog_instruction_pager" android:layout_width="wrap_content" android:layout_height="wrap_content" > </android.support.v4.view.ViewPager> <!-- this LinearLayout overlays the ViewPager --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <!-- This is just the container for the left and right arrows , it is allocated with only 20% of screen height--> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".2" android:orientation="horizontal" > <!-- I want to align this to the left and set width only 5% of the parent --> <ImageButton android:id="@+id/instruction_dialog_left_arrow" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".05" android:background="?android:attr/selectableItemBackground" android:scaleType="fitCenter" android:src="@drawable/arrow_left_grey" /> <!-- This is the dummy layout, standing in the middle of 2 arrows so they can be align to the left and right of the parent --> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".8" > </LinearLayout> <!-- I want to align this to the right and set width only 5% of the parent--> <ImageButton android:id="@+id/instruction_dialog_right_arrow" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight=".05" android:background="?android:attr/selectableItemBackground" android:scaleType="fitCenter" android:src="@drawable/arrow_right_grey" /> </LinearLayout> </LinearLayout> 

PS: ¿Hay algún diseño que permita alinear a los niños a la izquierda, derecha, abajo (como RelativeLayout) y también permite definir el ancho, la altura como porcentaje (como el atributo "weight" en LinearLayout)?

Gracias !

 Try the below code and see whether it works, This code will get the layout what you have attached <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/dialog_instruction_pager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/theme_blue" > </android.support.v4.view.ViewPager> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerInParent="true" android:background="@drawable/ic_launcher" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:background="@drawable/ic_launcher" /> </RelativeLayout> 

prueba esto

para evitar esta disposición es inútil (para los diseños falsos) u debe android:background="@android:color/transparent" y agregar la vista dummy en esa disposición linear porque la disposición no debe estar vacía .

para evitar pesas anidadas son malas para el rendimiento añadir un niño de diseño lineal. y leer mis comentarios en el código

y agregó la disposición de los padres para la imageview para hacer la imagen verticalmente centro

prueba este código

  <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/dialog_instruction_pager" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v4.view.ViewPager> <!-- this LinearLayout overlays the ViewPager --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <!-- This is just the container for the left and right arrows , it is allocated with only 20% of screen height--> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".2" android:orientation="horizontal"> <!--child layout to avoid nested weight with `waighSum` property--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="100"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="5" android:gravity="center_vertical" android:orientation="horizontal"> <!-- I want to align this to the left and set width only 5% of the parent --> <ImageButton android:id="@+id/instruction_dialog_left_arrow" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:scaleType="fitCenter" android:src="@drawable/icon_clock" /> </LinearLayout> <!-- This is the dummy layout, standing in the middle of 2 arrows so they can be align to the left and right of the parent --> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="90" android:background="@android:color/transparent">// this line also makes layout usefull <!-- dummy view becouse layout should not be empty --> <View android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="5" android:gravity="center_vertical" android:orientation="horizontal"> <!-- I want to align this to the right and set width only 5% of the parent--> <ImageButton android:id="@+id/instruction_dialog_right_arrow" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:scaleType="fitCenter" android:src="@drawable/icon_clock" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </FrameLayout> 

espero que funcione para ti

Google introdujo una nueva API llamada android.support.percent

1) PorcentajeRelativeLayout

2) PorcentajeFrameLayout

Añadir dependencia de compilación como

 compile 'com.android.support:percent:23.1.1' 

Puede especificar la dimensión en porcentaje para obtener ambos beneficios de RelativeLayout y porcentaje

  <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="match_parent"/> <TextView app:layout_widthPercent="40%" app:layout_heightPercent="40%" app:layout_marginTopPercent="15%" app:layout_marginLeftPercent="15%"/> </android.support.percent.PercentRelativeLayout/> 

Intente usar RelativeLayout y después ajuste la imagen izquierda alignParentLeft y la imagen derecha alignParentRight

  • Obtener el primer fotograma de una imagen GIF sin descargar todos los demás fotogramas
  • SQLiteOpenHelper - ¿Cómo se crea la base de datos?
  • Método de llamada dispatchKeyEvent dos veces
  • Cómo agregar funcionalidad de flecha de botón Volver en barra de navegación
  • El método getOrientation () de Android devuelve resultados incorrectos
  • Reloj de cuenta atrás en android
  • ¿Cómo puedo guardar el estado de mi webview al cambiar la orientación usando un webviewfragment
  • Cree un nuevo TextView mediante programación y luego muéstrelo debajo de otro TextView
  • ¿Cómo hacer que el botón de cancelar trabaje en llamada sdk vinculada en la vista web android?
  • Cómo pasar la identificación de correo electrónico que se sincronizarán en crear calendario de eventos en android?
  • Grabación de audio no para archivo en Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.