Ajouter un TextView comme enfant d'un TextView existant
Bonjour,
Un peu comme en DOM, je cherche à ajouter par le code Java un TextView à un TextView déjà existant, le tout étant dans un TableRow.
J'arrive à récupérer le TV existant et lui ajouter du texte en contenu, comme ci-dessous, mais je souhaite ajouter un autre TV (indépendant) au TV existant.
Ceci fonctionne mais hors de l'objectif :
Code:
1 2 3 4
| mUnits = (TextView) findViewById(R.id.textView7);
String g_units = "blah";
String current_Text = mUnits.getText().toString();
mUnits.setText(current_Text + " (" + g_units + ")"); |
Ce que je tente mais qui plante car apparemment, le TV n'est pas castable comme un ViewGroup :
Code:
1 2 3 4 5 6 7
| mUnits = (TextView) findViewById(R.id.textView7);
TextView units_view = new TextView(this);
String g_units = "blah";
units_view.setText(g_units);
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
units_view.setLayoutParams(textViewLayoutParams);
((ViewGroup) mUnits).addView(units_view); // Là, ça plante |
Puis-je garder ma structure XML en l'état et ajouter un enfant au TextView existant ou sinon, comment opérer ?
Merci d'avance