gpt4 book ai didi

android - 无法将 View 移动到 TableRow

转载 作者:行者123 更新时间:2023-11-30 01:22:55 25 4
gpt4 key购买 nike

我正在尝试扩充布局资源并将其中的一些 View 移动到表格中。我从他们的 parent 那里删除了 View ,然后将它们添加到表中。它有效,但我真正需要的是将它们添加到表中的 TableRow。但是,当我尝试这样做时,什么也没有出现。

为了简化示例,我只使用 xml 中的 View 而不是代码中生成的 View 。在下面的 xml 中,有一个带有一个 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);

有没有人有解决办法?

最佳答案

尝试将 Button 添加到这样的行中:

   row.addView(button,new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));

关于android - 无法将 View 移动到 TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com