如何在 Android 中包含布局片段

Program-Me-Rev

我正在尝试添加一个布局片段,但我不断收到错误消息:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这是片段包含在rev_lay_drawer_nav.xml 中的方式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/helpAboutLL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/about_bags_bttn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="About BAGS" />

        <Button
            android:id="@+id/help_bttn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/rev_dr_thinner_help_top"
            android:text="Help" />

    </LinearLayout>

</LinearLayout>  

我是这样称呼它的:

mContext = context;

revLayDrawerLayoutInflater = LayoutInflater.from( mContext );
revLayDrawerView = revLayDrawerLayoutInflater.inflate( R.layout.rev_lay_drawer_nav, null, false );

LinearLayout helpAboutLL = (LinearLayout) revLayDrawerView.findViewById(R.id.helpAboutLL);

LinearLayout revDrawerNavViewContainer = new LinearLayout(mContext);
revDrawerNavViewContainer.setOrientation(LinearLayout.VERTICAL);

revDrawerNavViewContainer.addView( helpAboutLL );  

正确的做法是什么?

一个板球运动员

您使用 findViewById 的事实意味着您已经拥有一个带有父布局的视图。

并且您无法将视图添加到另一个布局,而它具有另一个父视图。


不清楚为什么你需要嵌套的 LinearLayouts 所以改变你的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/helpAboutLL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/about_bags_bttn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="About BAGS" />

    <Button
        android:id="@+id/help_bttn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/rev_dr_thinner_help_top"
        android:text="Help" />

</LinearLayout>

并充气并添加

LinearLayout revDrawerNavViewContainer = new LinearLayout(mContext);
revDrawerNavViewContainer.setOrientation(LinearLayout.VERTICAL);

View helpAboutLL = revLayDrawerLayoutInflater.inflate( R.layout.rev_lay_drawer_nav, null, false );
revDrawerNavViewContainer.addView( helpAboutLL );  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中动态包含布局?

如何渗透Android片段中的布局的差异

如何在android中引用包含布局中的视图

如何在Android中以编程方式包含布局?

Android - 如何访问包含布局中的按钮

如何在Android中没有活动和片段的功能中使用布局组件?

如何在Android布局中更改XML片段元素的默认提示值

Android:如何在回收站布局适配器中动态填充片段?

如何在Android布局的片段中添加两个列表视图?

android-如何在已经膨胀的布局中膨胀地图片段?

如何从特定活动隐藏BaseActivity布局中包含的片段

如何在ViewPager Android中设置片段的动态高度包含RecycleView?

Android-如何从活动中包含的片段更新活动视图

如何在Android的TabLayout中替换片段

如何在android中从BottomSheetDialogFragment调用片段

如何在Android中重用片段的视图?

如何在Android中刷新片段

如何在Android的自定义对话框中隐藏包含布局

如何在包含textviews的android中更改整个线性布局的背景色

如何在android中获取布局的底部?

如何在android中设计布局

如何在Android中可见的布局

如何在android中制作全屏布局?

如何将XML布局包含到Android库中

Android XML:如何在包含的布局下方放置小部件

如何在android中的图形布局上查看布局?

如何在片段中成功实现android mapbox android sdk

Android-如何动态更改片段布局

Android:当布局中已经包含按钮时,如何以编程方式覆盖整个布局?