/ / Comment définir les marges de mise en page des zones de texte d'édition? - java, android, ellipse

Comment définir les marges de présentation des zones de texte d'édition? - java, android, ellipse

Dans la mise en page du tableau, j'ai un tableau et dans ce tableau, j'ai 6 zones de texte d'édition et je veux définir les marges de la mise en page pour ces 6 zones de texte d'édition

TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);

TableRow tr1=new TableRow(inventory.this);
tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));


tr1.setBackgroundColor(Color.BLACK);
EditText ed6=new EditText(inventory.this);
//ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
/*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/


ed6.setTextColor(Color.BLACK);
ed6.setBackgroundColor(Color.WHITE);


ed6.setText("1");
tr1.addView(ed6);



EditText ed7=new EditText(inventory.this);
//ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed7.setTextColor(Color.BLACK);
ed7.setBackgroundColor(Color.WHITE);
ed7.setText("2");

tr1.addView(ed7);

EditText ed8=new EditText(inventory.this);
//ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed8.setTextColor(Color.BLACK);
ed8.setBackgroundColor(Color.WHITE);
ed8.setText("3");

tr1.addView(ed8);

EditText ed9=new EditText(inventory.this);
//ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed9.setTextColor(Color.BLACK);
ed9.setBackgroundColor(Color.WHITE);
ed9.setText("4");

tr1.addView(ed9);

EditText ed10=new EditText(inventory.this);
//ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed10.setTextColor(Color.BLACK);
ed10.setText("5");
ed10.setBackgroundColor(Color.WHITE);

tr1.addView(ed10);

EditText ed11=new EditText(inventory.this);
//ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed11.setTextColor(Color.BLACK);
ed11.setText("6");
ed11.setBackgroundColor(Color.WHITE);

tr1.addView(ed11);

t1.addView(tr1);

Réponses:

8 pour la réponse № 1

Tout d’abord, vous devez savoir quelque chose: selon le Pages de développement Android officielles, Vues (et un TextView dérive de View) ne prennent pas en charge le paramètre Margin, mais ViewGroups (tels que LinearLayout, RelativeLayout etc ...) faire.

Donc, ce que vous pourriez faire est le suivant:

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);

Cela définirait la marge pour tous les enfants à 5 pixels - je l’ai essayé et cela a fonctionné pour moi (même si LinearLayout avec alignement vertical). Donnez-lui un coup de feu et laissez-moi savoir si je peux aider davantage :).

À votre santé,

Ready4Fajir


4 pour la réponse № 2

MODIFIER:
Je voudrais essayer avec le XML ci-dessous (vous "d, bien sûr, mettre à jour l'id" s etc.). La "magie" dans le XML est qu'il distribue toute la largeur disponible uniformément entre les TextView"s (et le EditText"s sur la deuxième rangée).

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The first "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="@+id/textView01" />

<!-- Here you"d add your other five TextView"s accordingly -->

</LinearLayout>

<!-- The second "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="@+id/editText01" />

<!-- Here you"d add your other five EditText"s accordingly -->

</LinearLayout>
</LinearLayout>

Dans votre code Java, vous pouvez ensuite accéder à vos vues EditText comme:

EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");

J'ai maintenant ignoré le fait que vous devez créer votre programme EditText "s par programme. vraiment, vraiment besoin de les créer en Java? (Pourquoi?)

ANCIENNE REPONSE:
Si vous voulez juste définir les marges de la mise en page dans votre vue EditText, vous pouvez utiliser la commande setMargins(left, top, right, bottom) appel de fonction sur le LayoutParams variable.

int left = 6;
int top = 12;
int right = 6;
int bottom = 6;

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);

EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);

Si vous souhaitez finalement répartir uniformément tout l’espace disponible entre les six vues EditText d’une rangée de tableau, je vous suggère de consulter le post suivant: TableLayout à 2 colonnes avec 50% exactement pour chaque colonne