【发布时间】:2017-03-13 00:09:08
【问题描述】:
好的,所以我有一个片段,我想在它的 xml 文件中包含另一个以编程方式膨胀的布局
片段:
public class zGoal_Fragment extends Fragment{
private LinearLayout todayView;
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.main_goal_view, container, false);
todayView = (LinearLayout)view.findViewById(R.id.todayView);
return view;
}
}
片段的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/todayView"
>
</LinearLayout>
和我希望以编程方式包含在上述 xml 中的 xml 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/goalEditLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/test_color_two"
>
</LinearLayout>
我尝试了几种不同的方法,所有这些方法都会导致“在空对象引用上”错误...
请帮助:))
【问题讨论】:
-
我已为您的查询添加了答案。您可以使用 ViewStubs 甚至更好,使用包含标签并在目标 xml 文件中添加另一个文件的布局...
标签: java android xml android-layout android-fragments