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