我有三个需要与屏幕对齐的文字视图。我无法对齐它们,见下文:
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 :(得分: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)