【问题标题】:Initializing a view without findViewById()在没有 findViewById() 的情况下初始化视图
【发布时间】:2014-08-22 11:40:21
【问题描述】:

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

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

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

record是fragment的布局xml文件中表格布局的id

<TableLayout

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

    </TableLayout>

提前致谢。

【问题讨论】:

    标签: java android layout view fragment


    【解决方案1】:

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

    为此,我使用了布局充气器

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多