studio percent linearlayout example constraint android android-layout android-linearlayout weight android-percent-library

android - percent - Reemplazo para el mecanismo de pesos lineales



percent layout android example (4)

Fondo:

  • Google sugiere evitar el uso de linealLayouts ponderados anidados debido al rendimiento.
  • El uso de LinLayout ponderado anidado es horrible de leer, escribir y mantener.
  • Todavía no hay una buena alternativa para colocar vistas que sean% del tamaño disponible. Solo las soluciones son pesos y utilizando OpenGL. Ni siquiera hay algo como el "viewBox" que se muestra en WPF / Silverlight para escalar automáticamente las cosas.

Esta es la razón por la que he decidido crear mi propio diseño, el cual usted le dice a cada uno de sus hijos exactamente cuál debe ser su peso (y el peso que lo rodea) en comparación con su tamaño.

Parece que he tenido éxito, pero por alguna razón creo que hay algunos errores que no puedo encontrar.

Uno de los errores es que textView, aunque le de mucho espacio, pone el texto en la parte superior en lugar de en el centro. Las vistas de imágenes por otra parte funcionan muy bien. Otro error es que si uso un diseño (por ejemplo, un frameLayout) dentro de mi diseño personalizado, no se mostrarán las vistas dentro de él (pero sí el diseño).

Por favor, ayúdame a averiguar por qué ocurre.

Cómo usarlo: en lugar del siguiente uso del diseño lineal (uso un XML largo a propósito, para mostrar cómo mi solución puede acortar las cosas):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="wrap_content" android:layout_height="0px" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" android:orientation="horizontal"> <View android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:layout_width="0px" android:layout_weight="1" android:layout_height="match_parent" android:text="@string/hello_world" android:background="#ffff0000" android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" /> <View android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <View android:layout_width="wrap_content" android:layout_height="0px" android:layout_weight="1" /> </LinearLayout>

Lo que hago es simplemente (la x es donde colocar la vista en la lista de pesos):

<com.example.weightedlayouttest.WeightedLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.weightedlayouttest" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="0px" android:layout_height="0px" app:horizontalWeights="1,1x,1" app:verticalWeights="1,1x,1" android:text="@string/hello_world" android:background="#ffff0000" android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" /> </com.example.weightedlayouttest.WeightedLayout>

Mi código del diseño especial es:

public class WeightedLayout extends ViewGroup { @Override protected WeightedLayout.LayoutParams generateDefaultLayoutParams() { return new WeightedLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); } @Override public WeightedLayout.LayoutParams generateLayoutParams(final AttributeSet attrs) { return new WeightedLayout.LayoutParams(getContext(),attrs); } @Override protected ViewGroup.LayoutParams generateLayoutParams(final android.view.ViewGroup.LayoutParams p) { return new WeightedLayout.LayoutParams(p.width,p.height); } @Override protected boolean checkLayoutParams(final android.view.ViewGroup.LayoutParams p) { final boolean isCorrectInstance=p instanceof WeightedLayout.LayoutParams; return isCorrectInstance; } public WeightedLayout(final Context context) { super(context); } public WeightedLayout(final Context context,final AttributeSet attrs) { super(context,attrs); } public WeightedLayout(final Context context,final AttributeSet attrs,final int defStyle) { super(context,attrs,defStyle); } @Override protected void onLayout(final boolean changed,final int l,final int t,final int r,final int b) { for(int i=0;i<this.getChildCount();++i) { final View v=getChildAt(i); final WeightedLayout.LayoutParams layoutParams=(WeightedLayout.LayoutParams)v.getLayoutParams(); // final int availableWidth=r-l; final int totalHorizontalWeights=layoutParams.getLeftHorizontalWeight()+layoutParams.getViewHorizontalWeight()+layoutParams.getRightHorizontalWeight(); final int left=l+layoutParams.getLeftHorizontalWeight()*availableWidth/totalHorizontalWeights; final int right=r-layoutParams.getRightHorizontalWeight()*availableWidth/totalHorizontalWeights; // final int availableHeight=b-t; final int totalVerticalWeights=layoutParams.getTopVerticalWeight()+layoutParams.getViewVerticalWeight()+layoutParams.getBottomVerticalWeight(); final int top=t+layoutParams.getTopVerticalWeight()*availableHeight/totalVerticalWeights; final int bottom=b-layoutParams.getBottomVerticalWeight()*availableHeight/totalVerticalWeights; // v.layout(left+getPaddingLeft(),top+getPaddingTop(),right+getPaddingRight(),bottom+getPaddingBottom()); } } // /////////////// // LayoutParams // // /////////////// public static class LayoutParams extends ViewGroup.LayoutParams { int _leftHorizontalWeight =0,_rightHorizontalWeight=0,_viewHorizontalWeight=0; int _topVerticalWeight =0,_bottomVerticalWeight=0,_viewVerticalWeight=0; public LayoutParams(final Context context,final AttributeSet attrs) { super(context,attrs); final TypedArray arr=context.obtainStyledAttributes(attrs,R.styleable.WeightedLayout_LayoutParams); { final String horizontalWeights=arr.getString(R.styleable.WeightedLayout_LayoutParams_horizontalWeights); // // handle horizontal weight: // final String[] words=horizontalWeights.split(","); boolean foundViewHorizontalWeight=false; int weight; for(final String word : words) { final int viewWeightIndex=word.lastIndexOf(''x''); if(viewWeightIndex>=0) { if(foundViewHorizontalWeight) throw new IllegalArgumentException("found more than one weights for the current view"); weight=Integer.parseInt(word.substring(0,viewWeightIndex)); setViewHorizontalWeight(weight); foundViewHorizontalWeight=true; } else { weight=Integer.parseInt(word); if(weight<0) throw new IllegalArgumentException("found negative weight:"+weight); if(foundViewHorizontalWeight) _rightHorizontalWeight+=weight; else _leftHorizontalWeight+=weight; } } if(!foundViewHorizontalWeight) throw new IllegalArgumentException("couldn''t find any weight for the current view. mark it with ''x'' next to the weight value"); } // // handle vertical weight: // { final String verticalWeights=arr.getString(R.styleable.WeightedLayout_LayoutParams_verticalWeights); final String[] words=verticalWeights.split(","); boolean foundViewVerticalWeight=false; int weight; for(final String word : words) { final int viewWeightIndex=word.lastIndexOf(''x''); if(viewWeightIndex>=0) { if(foundViewVerticalWeight) throw new IllegalArgumentException("found more than one weights for the current view"); weight=Integer.parseInt(word.substring(0,viewWeightIndex)); setViewVerticalWeight(weight); foundViewVerticalWeight=true; } else { weight=Integer.parseInt(word); if(weight<0) throw new IllegalArgumentException("found negative weight:"+weight); if(foundViewVerticalWeight) _bottomVerticalWeight+=weight; else _topVerticalWeight+=weight; } } if(!foundViewVerticalWeight) throw new IllegalArgumentException("couldn''t find any weight for the current view. mark it with ''x'' next to the weight value"); } // arr.recycle(); } public LayoutParams(final int width,final int height) { super(width,height); } public LayoutParams(final ViewGroup.LayoutParams source) { super(source); } public int getLeftHorizontalWeight() { return _leftHorizontalWeight; } public void setLeftHorizontalWeight(final int leftHorizontalWeight) { _leftHorizontalWeight=leftHorizontalWeight; } public int getRightHorizontalWeight() { return _rightHorizontalWeight; } public void setRightHorizontalWeight(final int rightHorizontalWeight) { if(rightHorizontalWeight<0) throw new IllegalArgumentException("negative weight :"+rightHorizontalWeight); _rightHorizontalWeight=rightHorizontalWeight; } public int getViewHorizontalWeight() { return _viewHorizontalWeight; } public void setViewHorizontalWeight(final int viewHorizontalWeight) { if(viewHorizontalWeight<0) throw new IllegalArgumentException("negative weight:"+viewHorizontalWeight); _viewHorizontalWeight=viewHorizontalWeight; } public int getTopVerticalWeight() { return _topVerticalWeight; } public void setTopVerticalWeight(final int topVerticalWeight) { if(topVerticalWeight<0) throw new IllegalArgumentException("negative weight :"+topVerticalWeight); _topVerticalWeight=topVerticalWeight; } public int getBottomVerticalWeight() { return _bottomVerticalWeight; } public void setBottomVerticalWeight(final int bottomVerticalWeight) { if(bottomVerticalWeight<0) throw new IllegalArgumentException("negative weight :"+bottomVerticalWeight); _bottomVerticalWeight=bottomVerticalWeight; } public int getViewVerticalWeight() { return _viewVerticalWeight; } public void setViewVerticalWeight(final int viewVerticalWeight) { if(viewVerticalWeight<0) throw new IllegalArgumentException("negative weight :"+viewVerticalWeight); _viewVerticalWeight=viewVerticalWeight; } } }


Acepté tu desafío e intenté crear el diseño que describiste en respuesta a mi comentario. Tienes razón. Es sorprendentemente difícil de lograr. Además de eso, me gusta disparar moscas domésticas. Así que me subí a bordo y se me ocurrió esta solución.

  1. Amplíe las clases de diseño existentes en lugar de crear las suyas desde cero. Comencé con RelativeLayout, pero todos pueden usar el mismo enfoque. Esto le brinda la capacidad de usar el comportamiento predeterminado para ese diseño en vistas secundarias que no desea manipular.

  2. Agregué cuatro atributos al diseño llamado arriba, izquierda, ancho y alto. Mi intención era imitar HTML permitiendo valores como "10%", "100px", "100dp", etc. En este momento, el único valor aceptado es un número entero que representa el% del padre. "20" = 20% del diseño.

  3. Para un mejor rendimiento, permito que el super.onLayout () se ejecute en todas sus iteraciones y solo manipulo las vistas con los atributos personalizados en su última pasada. Dado que estas vistas se colocarán y se escalarán independientemente de los hermanos, podemos moverlas una vez que todo lo demás se haya establecido.

Aquí está atts.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HtmlStyleLayout"> <attr name="top" format="integer"/> <attr name="left" format="integer"/> <attr name="height" format="integer"/> <attr name="width" format="integer"/> </declare-styleable> </resources>

Aquí está mi clase de diseño.

package com.example.helpso; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; public class HtmlStyleLayout extends RelativeLayout{ private int pass =0; @Override protected HtmlStyleLayout.LayoutParams generateDefaultLayoutParams() { return new HtmlStyleLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); } @Override public HtmlStyleLayout.LayoutParams generateLayoutParams(final AttributeSet attrs) { return new HtmlStyleLayout.LayoutParams(getContext(),attrs); } @Override protected RelativeLayout.LayoutParams generateLayoutParams(final android.view.ViewGroup.LayoutParams p) { return new HtmlStyleLayout.LayoutParams(p.width,p.height); } @Override protected boolean checkLayoutParams(final android.view.ViewGroup.LayoutParams p) { final boolean isCorrectInstance=p instanceof HtmlStyleLayout.LayoutParams; return isCorrectInstance; } public HtmlStyleLayout(Context context, AttributeSet attrs) { super(context, attrs); } public void setScaleType(View v){ try{ ((ImageView) v).setScaleType (ImageView.ScaleType.FIT_XY); }catch (Exception e){ // The view is not an ImageView } } @Override protected void onLayout(final boolean changed,final int l,final int t,final int r,final int b) { super.onLayout(changed, l, t, r, b); //Let the parent layout do it''s thing pass++; // After the last pass of final int childCount = this.getChildCount(); // the parent layout if(true){ // we do our thing for(int i=0;i<childCount;++i) { final View v=getChildAt(i); final HtmlStyleLayout.LayoutParams params = (HtmlStyleLayout.LayoutParams)v.getLayoutParams(); int newTop = v.getTop(); // set the default value int newLeft = v.getLeft(); // of these to the value int newBottom = v.getBottom(); // set by super.onLayout() int newRight= v.getRight(); boolean viewChanged = false; if(params.getTop() >= 0){ newTop = ( (int) ((b-t) * (params.getTop() * .01)) ); viewChanged = true; } if(params.getLeft() >= 0){ newLeft = ( (int) ((r-l) * (params.getLeft() * .01)) ); viewChanged = true; } if(params.getHeight() > 0){ newBottom = ( (int) ((int) newTop + ((b-t) * (params.getHeight() * .01))) ); setScaleType(v); // set the scale type to fitxy viewChanged = true; }else{ newBottom = (newTop + (v.getBottom() - v.getTop())); Log.i("heightElse","v.getBottom()=" + Integer.toString(v.getBottom()) + " v.getTop=" + Integer.toString(v.getTop())); } if(params.getWidth() > 0){ newRight = ( (int) ((int) newLeft + ((r-l) * (params.getWidth() * .01))) ); setScaleType(v); viewChanged = true; }else{ newRight = (newLeft + (v.getRight() - v.getLeft())); } // only call layout() if we changed something if(viewChanged) Log.i("SizeLocation", Integer.toString(i) + ": " + Integer.toString(newLeft) + ", " + Integer.toString(newTop) + ", " + Integer.toString(newRight) + ", " + Integer.toString(newBottom)); v.layout(newLeft, newTop, newRight, newBottom); } pass = 0; // reset the parent pass counter } } public class LayoutParams extends RelativeLayout.LayoutParams { private int top, left, width, height; public LayoutParams(final Context context, final AttributeSet atts) { super(context, atts); TypedArray a = context.obtainStyledAttributes(atts, R.styleable.HtmlStyleLayout); top = a.getInt(R.styleable.HtmlStyleLayout_top , -1); left = a.getInt(R.styleable.HtmlStyleLayout_left, -1); width = a.getInt(R.styleable.HtmlStyleLayout_width, -1); height = a.getInt(R.styleable.HtmlStyleLayout_height, -1); a.recycle(); } public LayoutParams(int w, int h) { super(w,h); Log.d("lp","2"); } public LayoutParams(ViewGroup.LayoutParams source) { super(source); Log.d("lp","3"); } public LayoutParams(ViewGroup.MarginLayoutParams source) { super(source); Log.d("lp","4"); } public int getTop(){ return top; } public int getLeft(){ return left; } public int getWidth(){ return width; } public int getHeight(){ return height; } } }

Aquí hay un ejemplo de actividad xml.

<com.example.helpso.HtmlStyleLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:html="http://schemas.android.com/apk/res/com.example.helpso" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:scaleType="fitXY" android:src="@drawable/bg" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/overlay" html:height="10" html:left="13" html:top="18" html:width="23" /> </com.example.helpso.HtmlStyleLayout>

Aquí están las imágenes que usé para las pruebas.

Si no establece un valor para un atributo particular, se usará su valor predeterminado. Entonces, si establece el ancho pero no la altura, la imagen se escalará en ancho y envolverá el contenido para la altura.

Carpeta de proyecto comprimida.

apk

Encontré la fuente del error. El problema es que estaba usando el recuento de elementos secundarios del diseño como indicador de cuántas llamadas a onLayout realizará. Esto no parece ser cierto en versiones anteriores de Android. Noté que en 2.1 onLayout solo se llama una vez. Así que cambié

if(pass == childCount){

a

if(true){

Y empezó a funcionar como se esperaba.

Todavía pienso que es beneficioso ajustar el diseño solo después de que se haga el súper. Solo necesito encontrar una mejor manera de saber cuándo es eso.

EDITAR

No me di cuenta de que su intención era unir imágenes con precisión píxel por píxel. Logré la precisión que está buscando mediante el uso de variables de precisión de doble flotación en lugar de enteros. Sin embargo, no podrá lograr esto mientras permite que sus imágenes se amplíen. Cuando una imagen se amplía, los píxeles se agregan en algún intervalo entre los píxeles existentes. El color de los nuevos píxeles es un promedio ponderado de los píxeles circundantes. Cuando escalas las imágenes de forma independiente, no comparten ninguna información. El resultado es que siempre tendrás algún artefacto en la costura. Agregue a eso el resultado del redondeo ya que no puede tener un píxel parcial y siempre tendrá una tolerancia de +/- 1 píxel.

Para verificar esto, puede intentar la misma tarea en su software de edición de fotos premium. Yo uso PhotoShop. Usando las mismas imágenes que en mi apk, las coloqué en archivos separados. Los escalé tanto en un 168% verticalmente como en un 127% horizontalmente. Luego los coloqué en un archivo e intenté alinearlos. El resultado fue exactamente el mismo que se ve en mi apk.

Para demostrar la precisión del diseño, agregué una segunda actividad a mi apk. En esta actividad no he escalado la imagen de fondo. Todo lo demás es exactamente lo mismo. El resultado es ininterrumpido.

También agregué un botón para mostrar / ocultar la imagen de superposición y otro para cambiar entre las actividades.

Actualicé tanto la aplicación como la carpeta de proyecto comprimida en mi disco de Google. Puedes conseguirlos por los enlaces de arriba.


Ahora hay una solución mejor que el diseño personalizado que he hecho:

PercentRelativeLayout

El tutorial se puede encontrar here y un repositorio se puede encontrar here .

Código de ejemplo:

<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"/> <ImageView app:layout_widthPercent="50%" app:layout_heightPercent="50%" app:layout_marginTopPercent="25%" app:layout_marginLeftPercent="25%"/> </android.support.percent.PercentFrameLayout/>

o:

<android.support.percent.PercentFrameLayout 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"/> <ImageView app:layout_widthPercent="50%" app:layout_heightPercent="50%" app:layout_marginTopPercent="25%" app:layout_marginLeftPercent="25%"/> </android.support.percent.PercentFrameLayout/>

Aunque me pregunto si puede manejar los problemas que he mostrado aquí.


Después de probar su código, simplemente encuentro la razón de los problemas que mencionó, y es porque en su diseño personalizado, usted solo diseña correctamente al niño, sin embargo, olvidó medirlo correctamente, lo que afectará directamente la jerarquía de dibujo, por lo que simplemente agrega el siguiente código, y funciona para mí.

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSize = MeasureSpec.getSize(widthMeasureSpec)-this.getPaddingRight()-this.getPaddingRight(); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec)-this.getPaddingTop()-this.getPaddingBottom(); int heightMode = MeasureSpec.getMode(heightMeasureSpec); if(heightMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.UNSPECIFIED) throw new IllegalArgumentException("the layout must have a exact size"); for (int i = 0; i < this.getChildCount(); ++i) { View child = this.getChildAt(i); LayoutParams lp = (LayoutParams)child.getLayoutParams(); int width = lp._viewHorizontalWeight * widthSize/(lp._leftHorizontalWeight+lp._rightHorizontalWeight+lp._viewHorizontalWeight); int height = lp._viewVerticalWeight * heightSize/(lp._topVerticalWeight+lp._bottomVerticalWeight+lp._viewVerticalWeight); child.measure(width | MeasureSpec.EXACTLY, height | MeasureSpec.EXACTLY); } this.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)); }


Propongo usar las siguientes optimizaciones:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:background="#ffff0000" android:gravity="center" android:textSize="20dp" android:textColor="#ff000000" /> </FrameLayout>

o use http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

o use TableLayout con layout_weight para filas y columnas

o utilizar GridLayout.