0

我有三個文本瀏覽需要與屏幕對齊。我不能將它們對齊,見下圖:動態添加與文本需要格式化的表 - - Android

https://www.dropbox.com/s/ab8pdhrpmjv2aql/Screenshot_2014-02-24-15-18-54.png

我需要能夠以編程方式將它們移動,並嘗試將它們匹配到靜態列標題。

創建由行:

   for (int j=0; j<events.size(); j++){ 
       String time = events.get(j).getTime(); 
       String event = events.get(j).getEvent(); 
       String location = events.get(j).getLocation(); 




       TableRow row = new TableRow(getActivity()); 

       TextView t = new TextView(getActivity()); 
       t.setText(time); 
       row.addView(t); 


       TextView e = new TextView(getActivity()); 
       e.setText(event); 
       row.addView(e); 

       TextView loc = new TextView(getActivity()); 
       t.setText(location); 
       row.addView(loc); 

       table.addView(row); 



      } 

和XML佈局:

<TableLayout 
     android:id="@+id/EventTable" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:stretchColumns="0" 
     android:layout_below="@+id/dateSpinner" 
     android:layout_marginTop="15dp"> 


     <TableRow>  
<!-- Column 1 -->  
<TextView   
android:id="@+id/columnTime"   
android:layout_width="0dip"   
android:layout_height="wrap_content"   
android:background="#CBCBCB"   
android:textColor="#000000"   
android:padding="10dip"   
android:layout_margin="4dip"   
android:layout_weight="1"   
android:text="@string/eventTableTime" />     
<!-- Column 2 -->  
<TextView   
android:id="@+id/columnEvent"   
android:layout_width="0dip"   
android:layout_height="wrap_content"   
android:background="#CBCBCB"   
android:textColor="#000000"   
android:padding="10dip"   
android:layout_margin="4dip"   
android:layout_weight="1"   
android:text="@string/eventTableEvent" />     
<!-- Column 3 -->  
<TextView   
android:id="@+id/columnLocation"   
android:layout_width="0dip"   
android:layout_height="wrap_content"   
android:background="#CBCBCB"   
android:textColor="#000000"   
android:padding="10dip"   
android:layout_margin="4dip"   
android:layout_weight="1"   
android:text="@string/eventTableLocation" />  
</TableRow> 


    </TableLayout > 

什麼想法?

感謝

+0

如果你正在嘗試添加行到tableLayout我建議你看看這個線程。 http://stackoverflow.com/questions/7279501/programatically-adding-tablerow-to-tablelayout-not-working – alexk

+0

製作一個外部xml佈局文件,該文件與您的普通文本視圖匹配相同的功能,但沒有背景,例如。你只是錯過了我相信的填充和邊距。 – Tristan

回答

1

嘗試定義每一行的佈局PARAMS:

TableRow row = new TableRow(getActivity()); 

TableRow.LayoutParams lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f); 
lp.setMargins(4, 4, 4, 4); 


TextView t = new TextView(getActivity()); 
t.setPadding(10, 10, 10, 10); 
t.setText("17:00"); 

row.addView(t, lp); 

t = new TextView(this); 
t.setText("Event 1"); 
t.setPadding(10, 10, 10, 10); 
t.setEllipsize(TruncateAt.MARQUEE); 
t.setSingleLine(true); 
t.setSelected(true); 
row.addView(t, lp); 

t = new TextView(getActivity()); 
t.setText("Location 1"); 
t.setEllipsize(TruncateAt.MARQUEE); 
t.setSelected(true); 
t.setSingleLine(true); 
t.setPadding(10, 10, 10, 10); 
row.addView(t, lp); 


table.addView(row); 

...只是作爲建議,最好能達到這個結果(特別是多條記錄),將使用一個ListView (或ListFragment)與最終一個AsyncLoader +適配器(最終是一個ViewHolder)

+0

嗨,我試過這個,它稍稍向左移動但不是很多。我已經嘗試更改爲0以及甚至-10等。它似乎有一個文本視圖的東西爲文本保留 – user2229747

+1

@ user2229747我讓我的答案更「冗長」:-)。這應該工作,讓我知道。 –

+0

非常感謝你!這讓我整日煩惱,我很感激。你真了不起:-) – user2229747