2011-01-27 18 views
6

En el diseño de la mesa tengo un TableRow y en ese TableRow tengo cuadros de texto 6 editar y quiero fijar los márgenes de la disposición para que los 6 cuadros de edición de texto¿Cómo establecer los márgenes de diseño de los cuadros de texto de edición?

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

Respuesta

4

EDIT:
me gustaría probar con el XML a continuación (por supuesto, actualizaría los id, etc.). La "magia" en el xml es que distribuye todo el ancho disponible de manera uniforme entre TextView (y EditText en la segunda fila).

<?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> 

En su código de Java que podría entonces tener acceso a sus puntos de vista EditText como:

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

ahora he ignorado el hecho de que usted necesita para crear su EditarTexto de programación. ¿Tiene realmente, Realmente necesita crearlos en Java? (¿Por qué?)

vieja respuesta:
Si lo que desea es establecer los márgenes de la disposición a la vista EditarTexto i quess se podía utilizar la llamada setMargins(left, top, right, bottom) función de la variable LayoutParams.

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 en última instancia, desea distribuir todo el espacio disponible de manera uniforme entre las seis vistas EditText en una fila de la tabla te sugeriría que echar un vistazo a la siguiente mensaje: 2-column TableLayout with 50% exactly for each column

+0

thnx para su señor respuesta ... pero que no está funcionando bien. – Arun

+0

¿Cuales son los sintomas? ¿Cuál es el comportamiento esperado y el real? – dbm

+0

en realidad tengo un diseño de tabla, ya que tengo dos filas de tabla. en la primera tabla, tengo seis columnas y cada columna tiene cada vista de texto. en la segunda fila tengo seis cuadros de cedittext y quiero que la segunda fila sea como la primera fila significa el mismo diseño marginal. Creé la primera fila en xml. Pero no puedo crear la segunda fila en xml bcoz. Tengo que hacer eso programáticamente. ... – Arun

8

en primer lugar algo que debe saber : Según el Official Android Dev Pages, las vistas (y un TextView se deriva de la vista) no admiten la configuración de Margen, pero ViewGroups (como LinearLayout, RelativeLayout etc ...) do.

Entonces, ¿qué se podría hacer es la siguiente:

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

Esto establecería el margen para todos los niños de 5 píxeles - Lo probé y funcionó para mí (aunque con un LinearLayout con alineación vertical). Pruébalo y avísame si puedo ayudarte más :).

Saludos,

Ready4Fajir

Cuestiones relacionadas