2012-06-23 5 views
2

Я нашел много решений о марже TableRow, но когда я попробовал строки, не оставляйте маржи вообще.Как установить разницу между TableRow android

Это мой код:

 TableLayout table = (TableLayout)findViewById(R.id.myTableLayout); 

     for (int i = 0; i < 3; i++) { 
      TableRow tr = new TableRow(this); 
      LayoutParams layoutParams = new LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
      int leftMargin=110; 
      int topMargin=100; 
      int rightMargin=100; 
      int bottomMargin=100; 
      layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 
      tr.setLayoutParams(layoutParams); 
      tr.setBackgroundResource(R.drawable.shelf_bar); 

      table.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 
     } 

Это мой ожидаемый результат:

enter image description here

Пожалуйста, кто-нибудь указать свою ошибку из. Благодаря

ответ

0

использовать это может

TableRow tr = new TableRow(this); 
TableLayout.LayoutParams tableRowParams= 
    new TableLayout.LayoutParams 
    (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT); 

     int leftMargin=10; 
     int topMargin=2; 
     int rightMargin=10; 
     int bottomMargin=2; 

tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); 

tr.setLayoutParams(tableRowParams); 
+0

Он все еще не работает !!! – Huppo

+0

вы можете попробовать с небольшим значком для теста, например, в коде –

+0

Мой код в настоящее время очень маленький, в XML-файле только простая таблица, а код в моем вопросе - все с кодом Java – Huppo

2

Это работает:

TableRow tr = new TableRow(...); 
TableLayout.LayoutParams lp = 
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
          TableLayout.LayoutParams.WRAP_CONTENT); 

lp.setMargins(10,10,10,10);    
tr.setLayoutParams(lp); 

------ 

// the key is here! 
yourTableLayoutInstance.addView(tr, lp); 

Вы должны добавить TableRow к TableLayout передавая параметры макета еще раз!

Смежные вопросы