无法将视图移动到表格
问题描述:
我正在尝试充气布局资源并将其中的某些视图移动到表格中。我从父母那里删除了这些意见,然后将它们添加到表格中。它可以工作,但我真正需要的是将它们添加到表中的TableRow中。但是,当我试图这样做时,什么都没有出现。无法将视图移动到表格
为了简化示例,我只是在xml中使用视图,而不是在代码中生成视图。在下面的XML中,有一个的TableRow一个TableLayout和ImageButton的:
<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);
有没有人有解决方案?
答
尝试将按钮添加到行这样的:
row.addView(button,new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
+0
工作!你让我开心! –
+0
很高兴工作:) – WannaBeGeek
你想添加按钮是动态的。即:添加用户希望的按钮数量。或者 你想执行简单的显示/隐藏功能吗? 如果是这样,那么您可以轻松设置按钮的可见性并轻松完成此操作! –
我试图将一堆动态生成的视图(如TextView,ImageView,CheckBox)移动到表中,因此可见性的更改不适用。 –