Как динамически включить макет слияния? - PullRequest
0 голосов
/ 24 января 2020

У меня есть макет main. xml

<?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>

Теперь в этом xml для linearLayout мне нужно включить другое слияние макетов. xml динамически.

<?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>

Код:

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

Выдает ошибку.

android .view.InflateException: двоичный файл XML строка файла № 2: может использоваться только с действительным ViewGroup root и attachToRoot = true

Ответы [ 3 ]

2 голосов
/ 24 января 2020

Ваша ошибка объясняет, что вы должны сделать. Замените это (обе строки):

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

на это (одна строка):

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

Таким образом, layoutInflater может правильно разрешать параметры макета.

0 голосов
/ 24 января 2020

попробуйте это ->

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));
0 голосов
/ 24 января 2020

<merge> это не то, что вы используете программно, потому что это не представление. Это атрибут времени компиляции, который указывает объединить данные макеты в любой другой макет, который их включает.

Документация слияния: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

...