在没有findViewById()的情况下初始化视图

时间:2014-07-01 19:55:31

标签: java android layout view fragment

我有一个活动和一个片段。现在在片段的onCreate()中,我想将行添加到片段的xml中定义的表格布局中。为此,我想将表格布局初始化为xml中定义的表格布局。在我的应用程序的视图中,我只有主要活动集的布局。我没有片段的视图,我现在无法设置它。所以当我写这篇

  TableLayout tableLayout = findViewById(R.id.record);

log cat中有错误。无法找到视图。我理解这个问题,但我无法在任何地方找到它的解决方案。

record是片段布局xml文件

中表格布局的id
<TableLayout

        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </TableLayout>

提前致谢。

1 个答案:

答案 0 :(得分:0)

通过大量研究,我终于找到了解决自己问题的方法。

为此,我使用了布局inflater

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view= inflater.inflate(R.layout.fragment_home_page, null);
    TableLayout tableLayout = (TableLayout)view.findViewById(R.id.record);