Android ViewGroup: ¿qué debo hacer en el overLayout ()?

Al extender una clase Android ViewGroup, ¿cuál es el propósito de la onLayout() ? Estoy haciendo un control personalizado en Android, pero por alguna razón el contenido (los objetos de View secundaria) no se muestra. Mi enfoque era ampliar la clase ViewGroup, agregando Child-Views a través del método addView() de ViewGroup. Entonces, en mi actividad principal, tengo el código siguiente:

 ChannelController myCC = new ChannelController(this); setContentView(myCC); 

ChannelController es el nombre de mi clase personalizada que extiende ViewGroup. Debo estar haciendo algo mal porque nada se muestra en la pantalla.

Entiendo que debo anular e implementar el método onLayout (), pero ¿con qué? Sé que hay una página entera dedicada a esto en el sitio dev.android, pero no me ayudó mucho, sobre todo porque soy un novato, supongo. Cualquier idea sería apreciada.

Para referencia, mi extensión ViewGroup se ve como a continuación:

 public class ChannelController extends ViewGroup { final String TAG = "JAL"; public ChannelController(Context c) { super(c); init(c); } public ChannelController(Context c, AttributeSet attibset) { super(c); init(c); } public ChannelController(Context c, AttributeSet attribset, int defStyle) { super(c); init(c); } public void init(Context c) { //RelativeLayout wrap = new RelativeLayout(c); RelativeLayout.LayoutParams wrapLP = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); RelativeLayout r1 = new RelativeLayout(c); RelativeLayout.LayoutParams r1LP = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); RelativeLayout r2 = new RelativeLayout(c); RelativeLayout.LayoutParams r2LP = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); TextView t = new TextView(c); RelativeLayout.LayoutParams tlp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); Button m = new Button(c); RelativeLayout.LayoutParams mlp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); Button s = new Button(c); RelativeLayout.LayoutParams slp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); SeekBar f = new SeekBar(c); RelativeLayout.LayoutParams flp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); t.setId(1); m.setId(2); s.setId(3); f.setId(4); r1.setId(5); r2.setId(6); t.setText("CHANNELNAME"); t.setTextColor(Color.BLACK); tlp.setMargins(30, 0, 0, 0); tlp.addRule(RelativeLayout.LEFT_OF, m.getId()); tlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); tlp.addRule(RelativeLayout.CENTER_VERTICAL); tlp.addRule(RelativeLayout.CENTER_HORIZONTAL); m.setText("M"); m.setBackgroundColor(Color.rgb(237, 155, 31)); m.setTextColor(Color.WHITE); mlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); mlp.addRule(RelativeLayout.ALIGN_PARENT_TOP); m.setTextSize(10); flp.addRule(RelativeLayout.LEFT_OF, s.getId()); flp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); flp.addRule(RelativeLayout.CENTER_VERTICAL); s.setText("S"); s.setTextSize(10); s.setBackgroundColor(Color.rgb(192, 48, 46)); s.setTextColor(Color.WHITE); slp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); slp.addRule(RelativeLayout.ALIGN_PARENT_TOP); r1.addView(t, tlp); r1.addView(m, mlp); r2.addView(f, flp); r2.addView(s, slp); r1.setBackgroundColor(Color.rgb(233, 242, 251)); r2.setBackgroundColor(Color.rgb(233, 242, 251)); r1LP.addRule(RelativeLayout.ALIGN_PARENT_TOP); r2LP.addRule(RelativeLayout.BELOW, r1.getId()); this.addView(r1, r1LP); this.addView(r2, r2LP); this.setLayoutParams(wrapLP); //this.addView(wrap); Log.i(TAG, "ChannelController constructor was called"); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub //super.onLayout(changed, l, t, r, b); } } 

¿Qué debo hacer en la anulación del método onLayout?

En onLayout necesitas llamar el método de layout a cada hijo de este ViewGroup y proporcionar la posición deseada (relativamente al padre) para ellos. Puede comprobar el código fuente de FrameLayout (una de las subclases más ViewGroup de ViewGroup ) para averiguar cómo funciona.

Aunque, si no necesita ningún diseño "especial", tiene otras opciones:

  • Extender alguna otra subclase de ViewGroup ( FrameLayout por ejemplo)
  • Utilice LayoutInflater si sólo necesita que su control se vea exactamente como en XML (que, creo, es exactamente su caso)
  • Configuración de la posición de la vista en el nivel 7 de la API mediante programación
  • CircledImageView siempre tiene una imagen de rectángulo delante del círculo
  • Dibujando una vista y todos sus hijos
  • ¿Cuántos WindowInsets hay?
  • Adición de TextViews al widget de pantalla de inicio mediante programación
  • Controlar la visibilidad de la vista desde un recurso
  • Mostrar una ubicación geográfica mediante webview
  • Cambiar el color del icono de desbordamiento de la barra de herramientas
  • Cómo establecer el ancho que es igual a otro widget en android
  • EditText no desplazable dentro de ScrollView
  • Cómo alinear el botón por centro y hacer offset
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.