相关文章推荐
慷慨的蚂蚁  ·  GitHub ...·  8 月前    · 
深情的黑框眼镜  ·  Java ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint1”
      android:layout_width="match_parent"
        android:layout_height="wrap_content"
     android:paddingBottom="@dimen/dimen_5"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
    android:id="@+id/textview1”
            android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="12dp"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:textSize="@dimen/ms_text_size"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
    <LinearLayout
    android:id="@+id/linearlayout1”
        app:layout_constraintTop_toBottomOf="@+id/textview1"
        app:layout_constraintStart_toStartOf="parent"
        android:orientation="vertical"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

Now in this xml for linearLayout i need to include another layout merge.xml dynamically.

<?xml version="1.0" encoding="utf-8"?>
        <merge xmlns:android="http://schemas.android.com/apk/res/android"
            <TextView
                android:id="@+id/text1”
                android:textStyle="bold"
                android:textSize="@dimen/txt_size_14"
                android:paddingStart="@dimen/dimen_14dp"
                android:paddingEnd="@dimen/dimen_14dp"
                android:textColor="@color/grey"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:id="@+id/text2”
                android:paddingStart="@dimen/dimen_14dp"
                android:paddingEnd="@dimen/dimen_14dp"
                android:layout_marginTop="@dimen/dimen_2"
                android:textSize="@dimen/txt_size_17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </merge>

Code :

LinearLayout linearLayout = rootView.findViewById(R.id.linearLayout1);
View view = layoutInflater.inflate(R.layout.merge, null, false );
linearLayout.addView(view);

It is giving error.

android.view.InflateException: Binary XML file line #2: can be used only with a valid ViewGroup root and attachToRoot=true

You can make a Fragment then include this fragment to a 'container' in your first Layout. (Container = LinearLayour/FameLayout...) – Aspicas Jan 24, 2020 at 13:28

Your error explains what you have to do. Replace this (both lines):

 View view = layoutInflater.inflate(R.layout.merge, null, false); 
 linearLayout.addView(view);

with this (one line):

layoutInflater.inflate(R.layout.merge, linearLayout, true);

So layoutInflater can properly resolve layout parameters.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. – stack Learner Jan 24, 2020 at 13:36 have you removed the second line (addView)? because in this case layoutinflater already adds it to the layout by specifying last argument as true. – Pawel Jan 24, 2020 at 13:38 yes removing addView Worked .But if i need to include this layout for 3 times it is only taking the third value. – stack Learner Jan 24, 2020 at 13:52

<merge> is not something you use programmatically because it's not a View. It's a compile-time attribute to indicate to merge the given layouts into any other layout that includes them.

Merge documentation: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

does that mean to include a layout programatically i need to give somae parent in merge.xml. – stack Learner Jan 24, 2020 at 13:36 This is not correct, you can add a layout with merge programmatically as the Pawel answer explain – cutiko Jan 24, 2020 at 13:45 @cutiko i was able to add with pawel answer but if i need to add it thrice it is only adding only the last value. – stack Learner Jan 24, 2020 at 14:21

try this -->

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.