1 pièce(s) jointe(s)
Comment mettre un GridLayout au lieu de LinearLayout
Bonjour
j'ai un probleme avec un GridLayout, je dois faire un affichage comme la photo ci_dessous mais j'arrive pas a le faire sous cette format seulement en linear layout
quelqu'un peut m'aider svp
Pièce jointe 346558
Voila mes codes
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package com.example.zied.coivoturageetudiant;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.zied.coivoturageetudiant.Entity.CategorieEntity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<CategorieEntity> listCategories;
LinearLayout layoutBtn;
ArrayList<LinearLayout> listlayoutBtnText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listCategories = new ArrayList<CategorieEntity>();
CategorieEntity fruit = new CategorieEntity(1, "Fruit", "desc cate 1", "https://openclipart.org/image/2400px/svg_to_png/280304/set-icone-frutta.png");
CategorieEntity legume = new CategorieEntity(2, "Legumes", "desc 2", "https://icon-icons.com/icons2/53/PNG/256/fruits_vegetables_10762.png");
listCategories.add(fruit);
listCategories.add(legume);
layoutBtn = (LinearLayout) findViewById(R.id.layoutImageButton);
listlayoutBtnText = new ArrayList<LinearLayout>();
for (int i = 0; i < listCategories.size(); i++) {
//imageCategories.setScaleType(ImageView.ScaleType.FIT_XY);
ImageView imageCategories = new ImageView(getApplicationContext());
GestionImage.loadImageFromUr(getApplicationContext(), listCategories.get(i).getImageCategorie(), imageCategories);
TextView libelleCategories = new TextView(getApplicationContext());
libelleCategories.setText(listCategories.get(i).getLibelleCategorie());
LinearLayout layoutBtnText = new LinearLayout(getApplicationContext());
layoutBtnText.addView(imageCategories);
layoutBtnText.addView(libelleCategories);
listlayoutBtnText.add(layoutBtnText);
final int finalI = i;
imageCategories.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long idCategorie = listCategories.get(finalI).getIdCategorie();
Intent intent = new Intent(getApplicationContext(), ListSelonCategorie.class);
intent.putExtra("idCategories", idCategorie);
startActivity(intent);
}
});
}
for (int i = 0; i < listlayoutBtnText.size(); i++) {
layoutBtn.addView(listlayoutBtnText.get(i));
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zied.coivoturageetudiant.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutImageButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</ScrollView>
</LinearLayout> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public class GestionImage {
public static void loadImageFromUr(Context context, String url, ImageView image) {
Glide.with(context)
.load(url)
.asBitmap()
.into(new BitmapImageViewTarget(image) {
@Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
}
});
}
} |
Nb : j'ai une table catégorie donc je dois recupere les images de la table , en cas d'ajout d'un nouveau catégorie sera automatiquement afficher