2011-01-27 57 views
6

在表格布局我有一个tablerow的,并在tablerow的我有6个编辑的文本框,我想设置布局利润率为6编辑文本框如何设置编辑文本框的布局边距?

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

回答

4

编辑:
我会尝试与下面的XML(你当然会更新ID等)。 xml中的“魔术”是它在TextView(和第二行的EditText's)之间均匀分配所有可用宽度。

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

在Java代码中,你可以再访问您的EditText的观点一样:

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

现在我忘记了,你需要以编程方式创建您的EditText的事实。你真的真的需要在Java中创建它们吗? (为什么?)

OLD答:
如果你只是想设置布局边距您的EditText视图我quess您可以使用setMargins(left, top, right, bottom)函数调用的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); 

如果你最终要均匀地分布在表行六个的EditText视图中的所有可用空间,我建议你看看下面的帖子:2-column TableLayout with 50% exactly for each column

+0

日Thnx您的答复先生......但它不工作 – Arun 2011-01-27 10:44:55

8

首先你应该知道:根据Official Android Dev Pages,视图(和一个TextView派生自视图)不支持设置边距,但视图组(如LinearLayoutRelativeLayout等)。

所以你可以做的是以下几点:

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

这将会为所有儿童边距设置为5个像素 - 我想它和它的工作对我来说(虽然与垂直对齐方式LinearLayout)。给它一个镜头,让我知道如果我可以进一步帮助:)。

干杯,

Ready4Fajir

相关问题