TableLayout
a を a内にネストしRelativeLayout
、後で Java コードで TableLayout を動的に編集したいと考えています。
私のXMLファイルは次のようになります。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_load_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoadDateActivity" >
<!-- few buttons and textviews -->
<TableLayout
android:id="@+id/activity_load_date_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button" >
</TableLayout>
</RelativeLayout>
Java コード:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_date);
//Do something with my Buttons and TextViews(this works fine)
tblLayout = (TableLayout) findViewById(R.id.activity_load_date_table_layout);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button_calc) {
for (int i = 0; i < listOfEntries.size(); i++) {
Entry temp = listOfEntries.get(i);
if (temp.getDate().getTime() >= startDate.getTime()
&& temp.getDate().getTime() <= endDate.getTime()) {
TableRow tr = new TableRow(this);
TextView comm = new TextView(this);
comm.setText(listOfEntries.get(i).getComment());
TextView val = new TextView(this);
val.setText(String.valueOf(listOfEntries.get(i).getValue()));
LayoutParams params = new LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1f);
tr.setLayoutParams(params);
tr.addView(comm);
tr.addView(val);
tblLayout.addView(tr);
}
}
tblLayout.invalidate(); //Shouldn't this redraw the entire TableLayout and therefore adding my TableRows? This is not working.
}
}
TextViews と Toasts を使用したさまざまなテストを通じて、tblLayout を埋める必要があり、TableRows がレイアウトに追加されていることがわかりました。機能していないのは、レイアウトの「再描画」だけです。どうすればそれを達成できますか?
編集:
どうやらこれが機能しない原因は、実際には TableRow に指定された LayoutParams でした。それらをコメントアウトすると、少なくとも画面に出力されました。しかし、それらは私が期待する場所ではありません。ボタンの下にあると思っていましたが、ボタンの上の左上隅にあります。これにより、TableLayout は実際には RelativeLayout と同じサイズですが、RelativeLayout の上に配置されていると思われます。そのため、エラーは XML ファイルにあるはずです。これを期待どおりに機能させるには、TableLayout をどのくらいの高さにする必要がありますか?
編集2:
android:layout_below
TableLayout に属性を追加する必要がありましたが、今では魅力的です!