IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

Layout Dynamique ?


Sujet :

Composants graphiques Android

  1. #1
    Nouveau membre du Club
    Inscrit en
    Mars 2011
    Messages
    140
    Détails du profil
    Informations forums :
    Inscription : Mars 2011
    Messages : 140
    Points : 35
    Points
    35
    Par défaut Layout Dynamique ?
    Bonjour à tous

    Est ce que je peux réaliser un Layout dynamique ?

    càd,

    j'ai un background,et je desire ajouter des images celon mon choix,(en utiliosant une base de données).

    par exemple : http://appaddict.net/wordpress/wp-co...helf_empty.png


    et on desire la remplir bar des image bouton (modifiable en fct d'une base de données)


    est ce que c'est réalisable ?

  2. #2
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    Tu trouveras un tutoriel ici sur le sujet

    http://mathias-seguy.developpez.com/...ihm-dynamique/
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  3. #3
    Nouveau membre du Club
    Inscrit en
    Mars 2011
    Messages
    140
    Détails du profil
    Informations forums :
    Inscription : Mars 2011
    Messages : 140
    Points : 35
    Points
    35
    Par défaut
    ok ,j'ai utilisé ce lien

    http://mathias-seguy.developpez.com/...ihm-dynamique/

    j'ai réalisé qlq modifications.

    voila mon code modifié

    /******************DynamicGui.java*********************/

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    package com.android2ee.tuto.gui;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.List;
     
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import android.app.Activity;
    import android.graphics.Typeface;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
     
    /**
     * @author Mathias Seguy (Android2EE)
     * @goals This class aims to:
     *        <ul>
     *        <li></li>
     *        </ul>
     */
    public class DynamicGui extends Activity {
    	/******************************************************************************************/
    	/** Attributes **************************************************************************/
    	/******************************************************************************************/
     
    	/**
             * tag used for the log (lower case because it's the naturla prameter of the log.*(tag,msg)
             * methods
             */
    	private final static String tag = "DynamicGui";
     
    	/**
             * The linear layout that contains the GUI builds dynamically
             */
    	private LinearLayout layoutOfDynamicContent;
    	/**
             * The hello message
             */
    	private TextView txvHello;
    	/**
             * The reload Button
             */
    	private Button btnReload;
     
    	/******************************************************************************************/
    	/** Life cycle Management **************************************************************************/
    	/******************************************************************************************/
     
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		// instanciate the components of the main.xml layout declaration
    		layoutOfDynamicContent = (LinearLayout) findViewById(R.id.layoutOfDynamicContent);
    		txvHello = (TextView) findViewById(R.id.txvHello);
    		btnReload = (Button) findViewById(R.id.btnReload);
    		// Add a click listener on the reload button
    		btnReload.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View arg0) {
    				buildDynamicView(GenerateData.getInstance().generateData());
    			}
    		});
    		// launch the first gui construction
    		buildDynamicView(GenerateData.getInstance().generateData());
    	}
     
    	/******************************************************************************************/
    	/** Dynamic GUIO building *****************************************************************/
    	/******************************************************************************************/
     
     
     
    	/**
             * Build the dynamic content
             * 
             * @param posts
             *            the list of posts to display
             */
    	private void buildDynamicView(List<JSONObject> posts) {
    		// Log.w(tag, "buildDynamicView");
    		// first delated the content:
    //		layoutOfDynamicContent.removeAllViews();
    		layoutOfDynamicContent.removeAllViewsInLayout();
     
    		// Then add a ScrollView within that layout:
    		ScrollView scvMain = new ScrollView(this);
     
    		scvMain.setHorizontalFadingEdgeEnabled(true);
    		scvMain.setFadingEdgeLength(20);
    		layoutOfDynamicContent.addView(scvMain, getParams(FILL, FILL));
    		// Then declare the LinearLayout where you'll build the content
    		LinearLayout lilContent = new LinearLayout(this);
    		lilContent.setOrientation(LinearLayout.VERTICAL);
     
    		// then browse the list of JSONObject and for each build the associated GUI
    		for (JSONObject post : posts) {
    			try {
    				lilContent.addView(buildPostGUI(post), getParams(FILL, WRAP));
    			} catch (JSONException e) {
    				Log.e(tag, e.toString());
    				// Do nothing or add an exception Panel in the gui
    				// anyway for a demonstration, i do nothing
    			}
    		}
     
    		// add the linearLayout to the scrollView
    		scvMain.addView(lilContent, getParams(FILL, FILL));
    	}
     
    	/**
             * Build the Gui associated to a post. This GUI is within a LinearLayout
             * 
             * @param post
             *            the JSONObject we have to build the GUI for
             * @return the linearlayout that shows the JSONObject
             * @throws JSONException
             */
    	private LinearLayout buildPostGUI(JSONObject post) throws JSONException {
    		// Log.w(tag, "buildPostGUI");
    		// define the layout
    		LinearLayout lilPost = new LinearLayout(this);
    		lilPost.setOrientation(LinearLayout.VERTICAL);
    		lilPost.setPadding(0, 15, 0, 0);
     
     
     
     
     
     
     
     
    		// then add the web picture
    		ImageView webPicture = new ImageView(this);
    		webPicture.setImageDrawable(getPicture(post.getString("fromWebPicture")));
     
    		//webPicture.setBackgroundColor(0xFFFFFFFF);
    		// for pictures you need to manage the LayoutParameter in a smartest way
    		LinearLayout.LayoutParams params = getParams(WRAP, WRAP);
    		// define its gravity and its margins
    		params.gravity = Gravity.CENTER_HORIZONTAL;
    		params.setMargins(50, 50, 50, 50);
    		// add the webpicture
    		lilPost.addView(webPicture, params);
     
    		// and return
    		return lilPost;
    	}
     
    	/**
             * Build the Gui associated to a message. This GUI is within a LinearLayout
             * 
             * @param message
             *            the JSONObject we have to build the GUI for
             * @return the linearlayout that shows the JSONObject
             * @throws JSONException
             */
     
     
     
    	/**
             * This method retrieve the drawable associated to the URL
             * 
             * @param urlPath
             *            the url of the picture
             * @return the drawable downloaded from the url
             */
    	private Drawable getPicture(String urlPath) {
    		// the drawable to return
    		Drawable drawable = null;
    		try {
    			// retrieve the URL
    			URL url = new URL(urlPath);
    			// Open an input stream on it
    			InputStream is = (InputStream) url.getContent();
    			// build the drawable from that input stream
    			drawable = Drawable.createFromStream(is, "src");
    		} catch (IOException e) {
    			Log.e(tag, e.toString());
    			e.printStackTrace();
    		}
    		return drawable;
    	}
     
    	/******************************************************************************************/
    	/** Layout parameters management **************************************************************************/
    	/******************************************************************************************/
     
    	/**
             * Constant layout get calls on layout method shorter To say Fill
             */
    	private final static Integer FILL = 0;
    	/**
             * Constant layout get calls on layout method shorter To say MATCH
             */
    	private final static Integer MATCH = 1;
    	/**
             * Constant layout get calls on layout method shorter To say WRAP
             */
    	private final static Integer WRAP = 1;
     
    	/**
             * The layout that Fill Weight Match Height (FWMH)
             * 
             * @param weight
             *            can be either Fill or Match constant from this, or
             *            LinearLayout.LayoutParams.WRAP_CONTENT
             * @param height
             *            can be either Fill or Match constant from this, or
             *            LinearLayout.LayoutParams.WRAP_CONTENT
             * @return a layout parameter that fill_parent in width and wrap_content in height
             */
    	private LinearLayout.LayoutParams getParams(int width, int height) {
    		int widthLayout = 0;
    		int heightLayout = 0;
    		// first retrieve the parameter of the layout for the width
    		if ((width == FILL) || (width == LinearLayout.LayoutParams.FILL_PARENT)) {
    			widthLayout = LinearLayout.LayoutParams.FILL_PARENT;
    		} else if ((width == WRAP) || (width == LinearLayout.LayoutParams.WRAP_CONTENT)) {
    			widthLayout = LinearLayout.LayoutParams.WRAP_CONTENT;
    		} else {
    			widthLayout = LinearLayout.LayoutParams.MATCH_PARENT;
    		}
    		// second retrieve the parameter of the layout for the height
    		if ((height == FILL) || (height == LinearLayout.LayoutParams.FILL_PARENT)) {
    			heightLayout = LinearLayout.LayoutParams.FILL_PARENT;
    		} else if ((height == WRAP) || (height == LinearLayout.LayoutParams.WRAP_CONTENT)) {
    			heightLayout = LinearLayout.LayoutParams.WRAP_CONTENT;
    		} else {
    			heightLayout = LinearLayout.LayoutParams.MATCH_PARENT;
    		}
     
    		return new LinearLayout.LayoutParams(widthLayout, heightLayout) {
    		};
    	}
    }

    /******************GenerateData.java*********************/

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    package com.android2ee.tuto.gui;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    /**
     * @author Mathias Seguy (Android2EE)
     * @goals This class aims to:
     *        <ul>
     *        <li></li>
     *        </ul>
     */
    public class GenerateData {
     
    	/******************************************************************************************/
    	/** Singleton Pattern (ok, it's an anti pattern, but not in that case) *******************/
    	/******************************************************************************************/
    	/**
             * The singleton object
             */
    	private static GenerateData singleton;
     
    	/**
             * @return the singleton
             */
    	public static GenerateData getInstance() {
    		if (singleton == null) {
    			singleton = new GenerateData();
    		}
    		return singleton;
    	}
     
    	/**
             * The usual private constructor
             */
    	private GenerateData() {
    		super();
    	}
     
    	/******************************************************************************************/
    	/** Normale Class **************************************************************************/
    	/******************************************************************************************/
    	/**
             * The tag for the log methods
             */
    	private static final String tag = "GenerateData";
    	/**
             * 6 items: Title Items to be used when generating data
             */
    	String[] titres = { "Java"};
    	/**
             * 6 Items: Descriptions to be used when generating data
             */
    	String[] descriptions = { "Le langage de programmation le plus fun du monde"};
    	/**
             * 6 Items: message's title to be used when generating data
             */
    	String[] titresMessages = { "Bonjour" };
    	/**
             * 7 items: message's body to be used when generating data
             */
    	String[] messages = { "Je vous trouve super"};
    	/**
             * 4 items: message's author to be used when generating data
             */
    	String[] from = { "Toto"};
    	/**
             * 4 items: message's picture to be used when generating data
             */
    	String[] drawables = { "icon1"};
    	/**
             * 5 items: message's picture from the web to be used when generating data
             */
    	String[] webDrawables = { "http://www.developpez.com/images/logos/java.gif"
    			};
     
    	/**
             * Generate a random set of data where each data respect the following structure:
             * <TABLE BORDER="2">
             * <CAPTION> Data structure </CAPTION>
             * <TR>
             * <TH>Key</TH>
             * <TH>Value Type</TH>
             * </TR>
             * <TR>
             * <TH>Title</TH>
             * <TD>String</TD>
             * </TR>
             * <TR>
             * <TH>Description</TH>
             * <TD>String</TD>
             * </TR>
             * <TR>
             * <TH>List<Message></TH>
             * <TD>
             * <TABLE BORDER="2">
             * <CAPTION> Data structure </CAPTION>
             * <TR>
             * <TH>Key</TH>
             * <TH>Value Type</TH>
             * </TR>
             * <TR>
             * <TH>MessagesNumber</TH>
             * <TD>Integer</TD>
             * </TR>
             * <TR>
             * <TH>Title</TH>
             * <TD>String</TD>
             * </TR>
             * <TR>
             * <TH>Message</TH>
             * <TD>String</TD>
             * </TR>
             * <TR>
             * <TH>From</TH>
             * <TD>String</TD>
             * </TR>
             * <TR>
             * <TH>FromPicture</TH>
             * <TD>Drawable</TD>
             * </TR>
             * </TABLE>
             * </TD>
             * </TR>
             * </TABLE>
             * 
             * @return the list of Data
             */
    	public List<JSONObject> generateData() {
    		List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
    		Double numberOfItems = (0.5 * 11);
    		for (int i = 0; i < numberOfItems.intValue(); i++) {
    			jsonObjects.add(generateOneData());
    		}
    		return jsonObjects;
    	}
     
    	/**
             * Build a Data
             * 
             * @return one Data
             */
    	private JSONObject generateOneData() {
    		try {
    			JSONObject jsonPost = new JSONObject();
    			int selectedInt = getRandomInt(1);
    			//String subject = titres[selectedInt];
    			jsonPost.put("name", "post");
    			//jsonPost.put("titre", subject);
    			//jsonPost.put("description", descriptions[selectedInt]);
    			jsonPost.put("fromWebPicture", webDrawables[selectedInt]);
    			jsonPost.put("fromWebPicture1", webDrawables[selectedInt]);
     
    			return jsonPost;
    		} catch (JSONException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			return null;
    		}
     
    	}
     
    	/**
             * return a random integer between 0 and MaxValues
             * 
             * @param maxValue
             * @return a random integer between 0 and MaxValues
             */
    	private int getRandomInt(int maxValue) {
    		Double random = (0.5 * maxValue);
    		return random.intValue();
    	}
    }


    /************main.xml*****************/

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:orientation="vertical" 
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent" 
    	android:id="@+id/mainlayout"
    	android:background="@drawable/menu">
    	<TextView android:layout_width="fill_parent"
    		android:layout_height="wrap_content" 
    		android:text="@string/hello"
    		android:id="@+id/txvHello" />
    	<Button android:text="@string/reload" 
    		android:id="@+id/btnReload"
    		android:layout_width="wrap_content" 
    		android:layout_height="wrap_content"></Button> 
    	<ScrollView android:layout_width="fill_parent" android:id="@+id/scrollView"
            android:layout_height="fill_parent">
    	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    		android:orientation="vertical" 
    		android:layout_width="fill_parent"
    		android:layout_height="fill_parent" 
    		android:id="@+id/layoutOfDynamicContent"
    		 android:background="@drawable/bib3">
    	</LinearLayout>
     
     
    	  </ScrollView>
    </LinearLayout>


    ce que j'ai compris :on ne peut ajouter qu'un seul ImageButton en utilisant mon code,


    Maintenant je veux ajouter plusieurs bouton sur la même ligne dans une LinearLayout.

    car les ImagesButton sont affichés l'un au dessous de l'autre.

    y-a-t-il une solution pour les placer les uns à cote des autres ?

  4. #4
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Citation Envoyé par bennour.mohamed Voir le message
    ce que j'ai compris :on ne peut ajouter qu'un seul ImageButton en utilisant mon code,
    Je ne sais pas... il semble qu'a chaque insertion de bouton tu commences par créer un LinearLayout vertical....
    Il vaudrait mieux créer un linear-layout horizontal une fois... et mettre tous les boutons dedans... enfin... j'en sais rien, je ne sais pas ce que tu veux obtenir au niveau UI....
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  5. #5
    Nouveau membre du Club
    Inscrit en
    Mars 2011
    Messages
    140
    Détails du profil
    Informations forums :
    Inscription : Mars 2011
    Messages : 140
    Points : 35
    Points
    35
    Par défaut
    ok,merci

Discussions similaires

  1. Réalisation Layout dynamique
    Par Simon MARQUIS dans le forum Composants graphiques
    Réponses: 9
    Dernier message: 16/07/2012, 18h25
  2. Layout Dynamique : texte et bouton dans un tablerow
    Par gilloddon dans le forum Composants graphiques
    Réponses: 4
    Dernier message: 26/03/2012, 21h58
  3. Modifier un titre dans un layout dynamiquement
    Par edblv dans le forum Ext JS / Sencha
    Réponses: 4
    Dernier message: 19/03/2012, 10h10
  4. Layout dynamique avec Tiles
    Par zulot dans le forum Développement Web en Java
    Réponses: 0
    Dernier message: 04/02/2011, 11h51
  5. [GridLayout] Layout dynamique
    Par mister3957 dans le forum Agents de placement/Fenêtres
    Réponses: 3
    Dernier message: 24/10/2005, 09h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo