Domanda

Sto cercando di aggiungere viste in modo dinamico a una LinearLayout. Vedo attraverso getChildCount () che le opinioni sono aggiunti al layout, ma anche chiamando invalidate () sul layout non darmi i bambino si presentò.

Mi sto perdendo qualcosa?

È stato utile?

Soluzione

Un paio di cose che si può controllare nel codice:

Questa autonomo esempio aggiunge una TextView dopo un breve ritardo, quando si comincia:

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);
    }
}

Altri suggerimenti

Ho avuto lo stesso problema e ho notato che il mio metodo onMeasure sovresposta () non è stato chiamato dopo l'invalidate. Così ho creato il mio subroutine nel LinearLayout e lo ha chiamato prima che il metodo invalidate ().

Ecco il codice per un LinearLayout verticale:

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);
}

Ho passato un sacco di tempo per risolvere anche questo problema. E ho trovato un metodo semplice di aggiornamento LinearLayout in 3 linee di codice

È necessario impostare il colore transperent in style.xml

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

E nel codice basta chiamare per impostare lo sfondo

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

Se si ha chiamata sfondo drawable

ll.setBackgroundResource(R.drawable.your_drawable);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top