2012-04-02 36 views
12

私はTableLayoutを動的に追加します。各TableRowにボタンを追加します。TableLayoutの列間にスペースを追加する

私の列(私のボタン)の間にスペースを追加したいのですが、どうしたらいいのか分かりません... 可能なすべての余白を変更しようとしましたが動作しません。マイbutton_table_row_category.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); 
    } 
} 

<?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ファイルからそれらを膨らませる私のコードでミスを犯しました
<?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

+0

ボタン名と列名の間にスペースを入れたいですか?正確な考えが得られていません。 – Dhruvisha

+0

@manmalボタンごとに新しい列があります。私は列の間にスペースを追加したい。行間では大丈夫です。 – Nico

答えて

13

TableLayoutの場合、ボタン自体が列です。つまり、ボタンの中にある程度のスペースを確保するようアドバイスする必要があります。レイアウトパラメータを使用してこれを行うことができます。 XMLで設定する方がはるかに簡単ですが、プログラムでも機能します。

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

ほとんどの寸法は、あなたがそれらを設定した場合のピクセルを取るのAndroidの属性:親がのTableRowである。この場合には - それはあなたが常にあなたがそれを適用する要素の親レイアウトのLayoutParamクラスを使用することが重要ですしたがって、私のdpToPixel()メソッドのようなものを使うべきです。さて、Androidでピクセル値を使ったことはありません!あなたは後でそれを後悔するでしょう。

右端のボタンにこのマージンがないようにするには、IFでチェックし、その上にLayoutParamを追加しないでください。 XMLで


ソリューション:

Layout params of loaded view are ignoredから取られた)膨らませながら、これを行う、あなたのXML定義属性を消去LayoutInflaterを避けるために:

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それは動作しますが、私はそれを手に入れません!私がandroid:layout_marginRight = "10dp"またはアンドロイド:layout_margin = "10dp"をbutton_table_row_category.xmlに入れようとすると、動作しません。どうすればXMLでそれを行うことができますか?再度、感謝します ! – Nico

+0

XMLソリューションで更新された回答:) – manmal

+0

MyViewがあなたのリンク上の問題に対して実際にレイアウトを表すクラスを持っていません。私はちょうどXMLを使用しています。その場合、どうすればいいのですか?私は驚いたことに、LayoutInflaterはXMLで定義された属性を消去しますか?どうして ? XMLを膨らませるのはどういう意味ですか? – Nico

8

TableLayoutsetColumnStretchable機能を使用してみてくださいテーブル行コンポーネントにコンポーネントの

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

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

</TableRow> 
+0

質問者がプログラム的にスペーシングを追加する方法を尋ねましたが、これは私を見て助けになりました。 XMLで回避する方法 –

0

をパディング右を追加します。それにcolumnnインデックスを与え、stretchableプロパティをtrueに設定します。

例: 3つの列がある場合。

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

上記は、tableLayoutの3つの列の間の等間隔を示します。

1

試してみてください。android:layout_marginRight="6dp"これは私に役立ちました。

関連する問題