【问题标题】:Dynamically added tablerow with text needs formatting - Android动态添加带有文本的表格行需要格式化 - Android
【发布时间】:2014-02-25 00:29:43
【问题描述】:

我有三个需要与屏幕对齐的文本视图。我无法对齐它们,见下文:

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 >

有什么想法吗?

谢谢

【问题讨论】:

  • 如果您尝试向 tableLayout 添加行,我建议您查看此线程。 stackoverflow.com/questions/7279501/…
  • 制作一个外部 xml 布局文件,它与您的常规 textview 具有相同的功能,但例如没有背景。我相信你只是缺少填充和边距。

标签: android android-layout tablelayout tablerow android-tablelayout


【解决方案1】:

尝试为每一行定义布局参数:

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 + Adapter(最终使用 ViewHolder)

【讨论】:

  • 嗨,我试过了,它稍微向左移动了一点,但不多。我也尝试过将其更改为 0 甚至 -10 等。似乎为文本保留的文本视图有一个文本框之类的东西
  • @user2229747 我的回答更“冗长”:-)..这应该可行,让我知道。
  • 非常感谢!这让我烦恼了一整天,我很感激。你太棒了:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 1970-01-01
相关资源
最近更新 更多