2011-11-30 34 views
0

這裏是我的xml:TableLayout沒有定心

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/test" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" > 
<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    > 
    <TextView 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
    <TextView 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" /> 
</TableRow> 

我動態生成的休息。

現在它在屏幕上水平居中,但一切都在左側。我知道它是一個簡單的修復,但我似乎無法做到。

這裏是我的手工代碼:

for(Item l : leaders) 
    { 
     // Make a new row 
     TableRow row = new TableRow(this); 
     row.setBackgroundColor(Color.DKGRAY); 

     TextView name= new TextView(this); 
     TextView score = new TextView(this); 

     createView(row, name, l.getName()); 
     createView(row, score, String.valueOf(l.getScore())); 

     name.setTextColor(Color.MAGENTA); // Just to distinguish between the columns 
     // Add row to table layout 
     tl.addView(row); 
    } 
} 

private void createView(TableRow row, TextView textView, String string) 
{ 
    textView.setText(string); 
    textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    textView.setTextColor(Color.CYAN); 
    textView.setTextSize(32); 
    textView.setPadding(20, 0, 0, 0); 
    row.setPadding(0, 1, 0, 1); 
    row.addView(textView); 
} 

而且,在給定的答案並沒有改變佈局。

+1

1 snap = 1000 words。 –

+1

當你說「它集中它,但一切都在左邊」時,它很難描繪出來。 @paresh正在制定的是哪一點?截圖會非常有幫助。 –

+0

拍攝手機的屏幕。表格行按照水平居中對齊,但是左對齊。 – segFault

回答

0

,您可以廣告layoutGravity中心表佈局在parrent中心佈局設置 爲

android:layout_gravity="center" 

可以設置重心

myTableLayout.setGravity(Gravity.CENTER); 
0

我覺得你應該在表中寫android:layout_gravity="center"佈局而不是android:gravity="center"

0

如果任何人有同樣的問題,關鍵是要設定

機器人:stretchColumns = 「0」

在TableLayout

機器人:layout_gravity =」中心「

android:layout_width =」match_parent「

行內的內容視圖。