studio linearlayout left horizontal ejemplo constraint caracteristicas java android android-linearlayout

java - left - Obtener elementos secundarios de LinearLayout



linearlayout android studio ejemplo (5)

¿Hay alguna manera de obtener un elemento hijo de LinearLayout? Mi código devuelve una vista (linearlayout), pero necesito tener acceso a elementos específicos dentro del diseño.

¿Alguna sugerencia?

(Sí, sé que podría usar findViewById, pero estoy creando los diseños / hijos en java, no en XML).


Creo que esto podría ayudar: findViewWithTag()

Establezca TAG en cada vista que agregue al diseño y luego obtenga esa vista por el TAG como lo haría con ID.


Evitaría agarrar estáticamente un elemento de los hijos de la vista. Puede que funcione ahora, pero hace que el código sea difícil de mantener y susceptible de afectar las versiones futuras. Como se indicó anteriormente, la forma correcta de hacerlo es establecer la etiqueta y obtener la vista por la etiqueta.


Puedes hacer esto

ViewGroup layoutCont= (ViewGroup) findViewById(R.id.linearLayout); getAllChildElements(layoutCont); public static final void getAllChildElements(ViewGroup layoutCont) { if (layoutCont == null) return; final int mCount = layoutCont.getChildCount(); // Loop through all of the children. for (int i = 0; i < mCount; ++i) { final View mChild = layoutCont.getChildAt(i); if (mChild instanceof ViewGroup) { // Recursively attempt another ViewGroup. setAppFont((ViewGroup) mChild, mFont); } else { // Set the font if it is a TextView. } } }


Siempre puedes hacer algo como esto:

LinearLayout layout = setupLayout(); int count = layout.getChildCount(); View v = null; for(int i=0; i<count; i++) { v = layout.getChildAt(i); //do something with your child element }


LinearLayout layout = (LinearLayout)findViewById([whatever]); for(int i=0;i<layout.getChildCount();i++) { Button b = (Button)layout.getChildAt(i) }

Si se trata de todos los botones, de lo contrario, se emiten para ver y verificar la clase

View v = (View)layout.getChildAt(i); if (v instanceof Button) { Button b = (Button) v; }