J'ai besoin d'aide pour mon Android app. J'ai besoin de gonfler une mise en page dans une autre et je ne sais pas comment je le fais. Mon code xml est le suivant:
item.xml - J'ai besoin de gonfler plusieurs xml (selon un nombre variable)
<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>
Ceci est mon xml principal:
<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>
Vous pourriez utiliser quelque chose comme
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);
Vous pouvez implémenter ceci comme ci-dessous:
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);
}
À un moment donné, vous devriez avoir accès à votre gonfleur dans votre activité, vous pouvez l'appeler avec ce code:
LayoutInflater li = LayoutInflater.from(context);
Où context est this ou this.getActivity () s'il s'agit d'un fragment. Puis gonflez votre layour avec:
View layout = li.inflate(R.layout.your_layout, null, false);
Ensuite, utilisez addView (layout) dans votre conteneur_destacado:
View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);
J'espère que ça aide.
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);
}
Le code Kotlin pour le faire:
val layoutToInflate =
this.layoutInflater.inflate(R.layout.ly_custom_layout,
null)
container_destacado.addView(layoutToInflate )
Vous avez le LinearLayout sur lequel vous voulez gonfler d'autres enfants:
LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);
Une fois que vous avez chargé le fichier item.xml avec un inflateur, vous pouvez simplement utiliser
container.addView(itemView);