Bonjour à vous !

Il est rare que je me tourne vers les forums car j'aime chercher et trouver la solution à mes problèmes. Pourtant là je sèche...
Voilà bientôt une heure que je bloque avec un soucis d'alignement.

Explication :

Je suis sur la partie JAVA d'une activité. Mon but est de créer, depuis le code java, un LinearLayout contenant 3 autres View, à savoir : 2 TextView et 1 EditText.

Ce qui donne sous forme de schéma :

<LinearLayout>
<TextView/>
<EditText/>
<TextView/>
</LinearLayout>

Mon problème est le suivant : Dans mes deux TextView j'ai mis un alignement = centre. Mais aucun moyen de voir le résultat lorsque je test l'application sur mon portable.

Voici ci-dessous la partie de mon code concerné :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 
 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, (int) px3 );
LinearLayout infoLayout = new LinearLayout(getApplicationContext());
params.setMargins(100, 50, 100, 50);
params.gravity = 4;
infoLayout.setLayoutParams(params);
infoLayout.setOrientation(LinearLayout.HORIZONTAL);
 
TextView addjeton = new TextView(getApplicationContext());
addjeton.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
addjeton.setBackgroundColor(getResources().getColor(R.color.red));
addjeton.setTextColor(getResources().getColor(R.color.white));
addjeton.setTextSize(px2);
addjeton.setText("Ajouter jeton(s)");
addjeton.setGravity(View.TEXT_ALIGNMENT_CENTER);
 
infoLayout.addView(addjeton);
 
EditText readNb = new EditText(getApplicationContext());
readNb.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
readNb.setInputType(NUMERIC);
readNb.setTextSize(px2);
 
infoLayout.addView(readNb);
 
TextView nbJeton = new TextView(getApplicationContext());
nbJeton.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
nbJeton.setBackgroundColor(getResources().getColor(R.color.black));
nbJeton.setTextColor(getResources().getColor(R.color.white));
nbJeton.setTextSize(px2);
nbJeton.setText(ens[i][3]+"/"+ens[i][4]);
nbJeton.setGravity(View.TEXT_ALIGNMENT_CENTER);
 
infoLayout.addView(nbJeton);
 
myLinearLayout.addView(infoLayout);
Il est fort possible que mon code ne soit pas entièrement cohérent, mais il est au moins fonctionnel (Appart le gravity="center" des deux TextView).

Pour les variables :
- px, px2, px3 sont des tailles en px dans un float.
- ens[][] tableau de String

Voilà, j'espère avoir était assez clair et si jamais vous avez d'autres questions n’hésitez pas, merci à ceux qui prendront le temps de m'aider.

Bonne journée à tous !