2012-04-02 75 views
12

我有一個TableLayout,我在那裏動態添加TableRows。在每個TableRow中,我添加一個Button。在TableLayout的列之間添加空格

我只是想在我的列(這是我的按鈕)之間添加一些空間,但我不知道如何... 我試圖改變所有可能的利潤率,但它不起作用: ( 所以也許我改變了我的代碼錯誤,我誇大他們從XML文件:

private void createButtons(final CategoryBean parentCategory) { 
    final List<CategoryBean> categoryList = parentCategory.getCategoryList(); 
    title.setText(parentCategory.getTitle()); 
    // TODO à revoir 
    int i = 0; 
    TableRow tr = null; 
    Set<TableRow> trList = new LinkedHashSet<TableRow>(); 
    for (final CategoryBean category : categoryList) { 

     TextView button = (TextView) inflater.inflate(R.layout.button_table_row_category, null); 
     button.setText(category.getTitle()); 
     if (i % 2 == 0) { 
      tr = (TableRow) inflater.inflate(R.layout.table_row_category, null); 
      tr.addView(button); 
     } else { 
      tr.addView(button); 
     } 

     trList.add(tr); 

     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       CategoryBean firstChild = category.getCategoryList() != null && !category.getCategoryList().isEmpty() ? category 
         .getCategoryList().get(0) : null; 
       if (firstChild != null && firstChild instanceof QuestionsBean) { 
        Intent intent = new Intent(CategoryActivity.this, QuestionsActivity.class); 
        intent.putExtra(MainActivity.CATEGORY, category); 
        startActivityForResult(intent, VisiteActivity.QUESTION_LIST_RETURN_CODE); 
       } else { 
        Intent intent = new Intent(CategoryActivity.this, CategoryActivity.class); 
        intent.putExtra(MainActivity.CATEGORY, category); 
        startActivityForResult(intent, VisiteActivity.CATEGORY_RETURN_CODE); 
       } 
      } 
     }); 
     i++; 
    } 
    for (TableRow tableRow : trList) { 
     categoryLaout.addView(tableRow); 
    } 
} 

我button_table_row_category.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/buttonTableRowCategory" 
    style="@style/ButtonsTableRowCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="@string/validate" /> 

我table_row_category.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableRowCategory" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="100dp" 
    android:gravity="center" 
    android:padding="5dp" > 

</TableRow> 

感謝您的幫助。

+0

你確實是說,你想在行之間添加空格(不是列?) – manmal 2012-04-02 11:17:38

+0

你想在按鈕和列名之間的空間還是什麼?沒有得到確切的想法.. – Dhruvisha 2012-04-02 11:29:29

+0

@manmal我有一個新的列每個按鈕。我想在列之間添加空格。行之間沒關係。 – Nico 2012-04-02 11:37:49

回答

13

對於TableLayout,Buttons本身就是列。這意味着你必須建議按鈕保持一定的空間。你可以通過使用佈局參數來做到這一點。它們在XML中設置要容易得多,但它也可以以編程方式工作。你總是使用LayoutParam類,你運用它的元素的父佈局的是非常重要的 - 在這種情況下,父親是一個的TableRow:在Android的

// Within createButtons(): 
android.widget.TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams(); 
p.rightMargin = DisplayHelper.dpToPixel(10, getContext()); // right-margin = 10dp 
button.setLayoutParams(p); 

// DisplayHelper: 
private static Float scale; 
public static int dpToPixel(int dp, Context context) { 
    if (scale == null) 
     scale = context.getResources().getDisplayMetrics().density; 
    return (int) ((float) dp * scale); 
} 

大多數維度的屬性,如果你將它們取像素以編程方式 - 因此你應該使用像我的dpToPixel()方法。請不要在Android中使用像素值!稍後你會後悔的。

如果您不想讓最右邊的按鈕具有此邊距,只需檢查一個IF並且不要在其上添加LayoutParam。


解決方案在XML:

爲了避免LayoutInflater擦掉你的XML定義的屬性,這樣做,而吹脹(從Layout params of loaded view are ignored拍攝):

View view = inflater.inflate(R.layout.item /* resource id */, 
            MyView.this /* parent */, 
            false /*attachToRoot*/); 

替代:像這樣使用GridView:Android: Simple GridView that displays text in the grids

+0

感謝Manmal它的工作原理,但我完全不明白!當我嘗試在我的button_table_row_category.xml中放置android:layout_marginRight =「10dp」或android:layout_margin =「10dp」時,它不起作用。我怎麼能在XML中做到這一點?再次感謝 ! – Nico 2012-04-03 14:14:18

+0

用XML解決方案更新了答案:) – manmal 2012-04-03 14:34:01

+0

實際上我沒有一個代表我的佈局的類,因爲MyView會爲您的鏈接上的問題提供解決方案。在這種情況下,我只是使用XML,我該怎麼做。我也很驚訝,LayoutInflater會擦除我定義的XML屬性?爲什麼 ?那麼膨脹XML有什麼意義呢? – Nico 2012-04-03 14:51:15

8

表中的行組件

<TableRow 
    android:id="@+id/tableRow1"> 

    <TextView 
     android:id="@+id/textView1" 
     android:paddingRight="20dp" /> 

</TableRow> 
+0

雖然提問者詢問如何以編程方式添加間距,但這幫助我尋找在XML中使用一種解決方法。 – 2017-12-26 07:51:49

0

嘗試使用TableLayoutsetColumnStretchable功能添加填充權的一個組成部分。給它一個columnn索引並將其可伸展屬性設置爲true。

例如,如果你有3列。

TableLayout tblLayout; 
tblLayout.setColumnStretchable(0, true); 
tblLayout.setColumnStretchable(1, true); 
tblLayout.setColumnStretchable(2, true); 

以上將給你所有3列tableLayout之間的等距。

1

嘗試android:layout_marginRight="6dp"這工作適合我。