2010-03-18 8 views
10

Estoy intentando agregar vistas dinámicamente a una distribución lineal. Veo a través de getChildCount() que las vistas se agregan al diseño, pero incluso llamar a invalidate() en el diseño no me da los elementos secundarios que se muestran.Actualizar un LinearLayout después de agregar una vista

¿Echo de menos algo?

+1

Muéstranos algunos códigos, ya que podría estar haciendo mal. – Pentium10

+0

I Simplemente crea una vista, agrégala a linearlayout y llama a invalidate(). – lbedogni

Respuesta

19

Un par de cosas que usted puede comprobar en el código:

Este auto contenida ejemplo añade un TextView después de un breve retraso cuando se inicia:

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class ProgrammticView extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final LinearLayout layout = new LinearLayout(this); 
     layout.setLayoutParams(new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.FILL_PARENT, 
       ViewGroup.LayoutParams.FILL_PARENT)); 

     setContentView(layout); 

     // This is just going to programatically add a view after a short delay. 
     Timer timing = new Timer(); 
     timing.schedule(new TimerTask() { 

      @Override 
      public void run() { 
       final TextView child = new TextView(ProgrammticView.this); 
       child.setText("Hello World!"); 
       child.setLayoutParams(new ViewGroup.LayoutParams(
         ViewGroup.LayoutParams.FILL_PARENT, 
         ViewGroup.LayoutParams.WRAP_CONTENT)); 

       // When adding another view, make sure you do it on the UI 
       // thread. 
       layout.post(new Runnable() { 

        public void run() { 
         layout.addView(child); 
        } 
       }); 
      } 
     }, 5000); 
    } 
} 
+0

Hago nuevas formas dentro de un Manejador. El hecho extraño es que la primera forma se dibuja en la distribución lineal, pero las siguientes no. – lbedogni

+0

compruebe que ha agregado android: orientation = "vertical" a LinearLayout – karabara

2

que tenían el mismo problema y se dio cuenta de que mi método sobreescrito onMeasure() no fue llamado después de la invalida. Así que creé mi propia subrutina en LinearLayout y la llamé antes del método invalidate().

Este es el código para un LinearLayout verticales:

private void measure() { 
    if (this.getOrientation() == LinearLayout.VERTICAL) { 
     int h = 0; 
     int w = 0; 
     this.measureChildren(0, 0); 
     for (int i = 0; i < this.getChildCount(); i++) { 
      View v = this.getChildAt(i); 
      h += v.getMeasuredHeight(); 
      w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w; 
     } 
     height = (h < height) ? height : h; 
     width = (w < width) ? width : w; 
    } 
    this.setMeasuredDimension(width, height); 
} 
+0

¿de dónde obtuvo la altura y el ancho? –

0

Pasé un montón de tiempo para resolver este problema también. Y he encontrado un método sencillo de actualización LinearLayout en 3 líneas de código

Debe establecer el color transperent en style.xml

<color name="transparent">#00000000</color> 

y en el código a llamarlo para establecer fondo

LinearLayout ll = (LinearLayout) findViewById(R.id.noteList); 
ll.setBackgroundColor(getResources().getColor(R.color.transparent)); 
ll.invalidate(); 

Si tiene fondo extraíble, llame al

ll.setBackgroundResource(R.drawable.your_drawable); 
Cuestiones relacionadas