2011-06-16 14 views
17

Estoy tratando de agregar contenido dinámico a una vista que se ha creado con XML.Cómo agrego elementos dinámicamente a una vista creada con XML

Tengo una vista "profile_list" que tiene un ScrollView al que me gusta agregar algunos elementos.

Aquí hay un código de lo que estoy tratando de hacer.

// Find the ScrollView 
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1); 

// Create a LinearLayout element 
LinearLayout ll = new LinearLayout(this); 
ll.setOrientation(LinearLayout.VERTICAL); 

// Add text 
tv = new TextView(this); 
tv.setText("my text"); 
ll.addView(tv); 

// Add the LinearLayout element to the ScrollView 
sv.addView(ll); 

// Display the view 
setContentView(R.layout.profile_list); 

El plan es añadir un TableLayout y llenarlo de forma dinámica y no sólo un texto de relleno pero primero hay que conseguir que esto funcione.

Cualquier ayuda es bienvenida.

Saludos cordiales Olle

he encontrado la solución!

Estúpido, ¡había dejado un elemento en mi ScrollView en mi archivo XML!

De todos modos ella es un ejemplo de trabajo:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.profile_list, null); 

    // Find the ScrollView 
    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1); 

    // Create a LinearLayout element 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    // Add text 
    TextView tv = new TextView(this); 
    tv.setText("my text"); 
    ll.addView(tv); 

    // Add the LinearLayout element to the ScrollView 
    sv.addView(ll); 

    // Display the view 
    setContentView(v); 

Y el archivo XML para que coincida con

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:clickable="true" 
     android:layout_weight="1" 
     android:layout_width="fill_parent" 
     android:layout_marginBottom="50px" 
     android:layout_height="fill_parent"> 
    </ScrollView> 

</LinearLayout> 

Esperamos que esto ayude a alguien. Gracias por toda la ayuda!

+0

no entiendo en silencio lo que quiere hacer. ¿Qué tal crear una vista personalizada? Creo que es mejor – Hein

+0

¿Por visión personalizada quieres decir crear toda la vista de forma dinámica? Soy bastante nuevo en su caso, así que perdóname si me haces una pregunta estúpida. Intenté esto pero no pude lograr que el diseño y el diseño funcionen de esa manera, así que comencé de nuevo. – Olle

Respuesta

6

La manera en la que agrega elementos a la vista es básicamente correcta: simplemente obtiene el objeto contenedor por su ID y luego lo agrega.

Parece que está configurando la vista de contenido como una vista incorrecta. Debe inflar la vista, agregar los elementos secundarios y configurarla como vista de contenido, o configurar la vista de contenido desde una ID de recurso y luego realizar la manipulación. Lo que estás haciendo es manipular una vista y luego agregar una diferente. Intenta mover tu setContentView (...) al comienzo del bloque.

+1

No funcionó para mí. Intenté esto en la parte superior 'LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE); Ver v = inflater.inflate (R.layout.profile_list, null); // Buscar el ScrollView ScrollView sv = (ScrollView) v.findViewById (R.id.scrollView1); 'Y ponen el' // Mostrar el setContentView vista (v); 'última – Olle

+1

Gran gracias !!! Si pudiera, votaría por su publicación. – Olle

Cuestiones relacionadas