Подтвердить что ты не робот

Наложение макета программно в другой макет

Мне нужна помощь в моем приложении для Android. Мне нужно раздуть макет в другом макете, и я не знаю, как это сделать. Мой xml-код:

  • item.xml - Мне нужно раздуть несколько xml (в зависимости от числа переменных)

        <RelativeLayout
             android:id="@+id/cartel_N1"
             android:layout_width="150dp"
             android:layout_height="match_parent"
             android:background="@color/tabhost_background_pressed"
             android:layout_marginRight="22dp"
             android:orientation="vertical" >
    
        <ImageView 
            android:id="@+id/img_N1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/mcdonalds_icon" />
    
        <TextView 
            android:id="@+id/title_N1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="McDonals del CC Alcampo"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="15sp" />
    
        <TextView 
            android:id="@+id/categoria_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="CATEGORIA"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />
    
        <RelativeLayout 
            android:id="@+id/stars_and_distance_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/stars_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stars_25"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7dp" />
    
    
            <TextView
                android:id="@+id/distance_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="200m"
                android:textSize="12sp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:gravity="center_vertical"
                android:layout_marginTop="3dp" />
    
    
            <ImageView
                android:id="@+id/icon_pos_N1"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:src="@drawable/marker_distance"
                android:layout_toLeftOf="@id/distance_N1"
                android:layout_marginTop="7dp" />
    
        </RelativeLayout><LinearLayout
        android:id="@+id/cartel_N1"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@color/tabhost_background_pressed"
        android:layout_marginRight="22dp"
        android:orientation="vertical" >
    
        <ImageView 
            android:id="@+id/img_N1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="15dp"
            android:src="@drawable/mcdonalds_icon" />
    
        <TextView 
            android:id="@+id/title_N1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="McDonals del CC Alcampo"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:textSize="15sp" />
    
        <TextView 
            android:id="@+id/categoria_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="CATEGORIA"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />
    
        <RelativeLayout 
            android:id="@+id/stars_and_distance_N1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/stars_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/stars_25"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="7dp" />
    
    
            <TextView
                android:id="@+id/distance_N1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="200m"
                android:textSize="12sp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:gravity="center_vertical"
                android:layout_marginTop="3dp" />
    
    
            <ImageView
                android:id="@+id/icon_pos_N1"
                android:layout_width="10dp"
                android:layout_height="10dp"
                android:src="@drawable/marker_distance"
                android:layout_toLeftOf="@id/distance_N1"
                android:layout_marginTop="7dp" />
    
        </RelativeLayout>
    
  • Это мой основной xml:

    <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
      <LinearLayout
           android:id="@+id/container_destacado"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical" >
    
               <!-- Inflate multiple xml file here -->
    
          </LinearLayout>
    </ScrollView>
    
4b9b3361

Ответ 1

Вы могли бы использовать что-то вроде

LayoutInflater inflater = LayoutInflater.from(context);

//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout

View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);

Ответ 2

Вы можете реализовать это, как показано ниже:

LayoutInflater linf;
LinearLayout rr;

linf = (LayoutInflater) getApplicationContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);

rr = (LinearLayout) findViewById(R.id.container_destacado);

for (int i = 1; i < NoOfTimes; i++) {

    final View v = linf.inflate(R.layout.item, null);
    rr.addView(v);
}

Ответ 3

В какой-то момент вы должны получить доступ к вашему надувнику внутри своей деятельности, вы можете позвонить ему с помощью этого кода:

LayoutInflater li = LayoutInflater.from(context);

Где контекст - это или this.getActivity(), если это фрагмент. Затем раздуйте свой план:

View layout = li.inflate(R.layout.your_layout, null, false);

Затем используйте addView (layout) для вашего container_destacado:

View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);

Надеюсь, поможет.

Ответ 4

ll = (LinearLayout) findViewById(R.id.container_destacado);  // ll is the layout where your inflated layout will be added 
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
    while (pos < noOfTimes) 
    {
        View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate 
        myView.setId(pos);
        /*
           You can change TextView text and ImageView images here e.g.
           TextView tv = (TextView)myView.findViewById(R.id.title_N1);
           tv.setText(pos);

        */
        pos++;
        ll.addView(myView);
    }

Ответ 5

У вас есть LinearLayout, по которому вы хотите надуть других детей:

LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);

После того, как вы загрузили item.xml с инфлятором, вы можете просто использовать

container.addView(itemView);

Ответ 6

Код Котлина для этого:

 val layoutToInflate = 
 this.layoutInflater.inflate(R.layout.ly_custom_layout, 
  null)
container_destacado.addView(layoutToInflate )