レイアウトリソースを膨らませて、その中のいくつかのビューをテーブルに移動しようとしています。私は両親からビューを削除し、それをテーブルに追加します。それは動作しますが、本当に必要なのはテーブルのTableRowにテーブルを追加することです。しかし、私がそれをやろうとすると何も現れなかった。ビューをTableRowに移動できません
例を単純化するために、コードで生成されたものの代わりにXMLでビューを使用するだけです。次のXMLでは、1のTableRowとのImageButtonとTableLayoutがあります
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/table">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/row_1"></TableRow>
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
</RelativeLayout>
私はテーブルにボタンを移動した場合、それは動作します:
Button button = (Button) getActivity().findViewById(R.id.button);
TableLayout table = (TableLayout) getActivity().findViewById(R.id.table);
TableRow row = (TableRow) getActivity().findViewById(R.id.row_1);
((ViewGroup)button.getParent()).removeView(button);
table.addView(button);
をしかし、私はそれを移動した場合TableRow、ボタンは表示されません。
row.addView(button);
解決方法はありますか?
ボタンの追加が動的になりたいですか。つまり、ユーザーが望む数だけボタンを追加します。または 簡単な表示/非表示機能を実行しますか? そうならば、ボタンの可視性を簡単に設定し、簡単にこれを行うことができます! –
TextView、ImageView、CheckBoxなどの動的に生成されたビューをテーブルに移動しようとしているため、可視性の変更は適用されません。 –