2012-05-09 3 views
0

Dans mon code, j'ajoute une image et du texte sous l'image. J'ai un XML avec un RelativeLayout qui contient un ImageView et un TextView. Dynamiquement, je gonfle cette mise en page xml et l'ajoute à un LinearLayout qui est dans le XML racine. Le problème auquel je suis confronté est que si j'ajoute plus d'image que l'écran peut contenir (en largeur), la dernière image devient petite. Je veux être en mesure d'avoir la même structure et ajouter une nouvelle ligne, par exemple je veux que chaque rangée ait un maximum de 4 images, comment puis-je y parvenir?Android - Ajouter dynamiquement plusieurs vues

Ci-dessous est ma mise en page XML racine:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
<TextView 
    android:id="@+id/text" 
    android:text="Friends who likes this:" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/> 
<LinearLayout 
    android:id="@+id/wrapper" 
    android:layout_below="@+id/text" 
    android:layout_marginTop="5dip" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:orientation="horizontal"/> 
</RelativeLayout> 

Et voici ma mise en page XML où je le imageview et textview:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/root" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
<ImageView 
    android:id="@+id/image" 
    android:layout_width="35dip" 
    android:layout_height="35dip" 
    android:layout_marginLeft="3dip" 
    android:src="@drawable/default_profile_picture"/> 
<TextView 
    android:id="@+id/username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/image" 
    android:layout_marginTop="2dip" 
    android:layout_marginLeft="3dip" 
    android:textSize="9sp" 
    android:text="Damian M."/> 
</RelativeLayout> 

et le code Java pour gonfler ceci:

LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.likes_view, this, true); 
    LinearLayout wrapper = (LinearLayout) findViewById(R.id.wrapper); 
    TextView text = (TextView) findViewById(R.id.text); 
    text.setTypeface(tf); 
    if(users.isEmpty()){ 
     text.setVisibility(View.INVISIBLE); 
    } 

    RelativeLayout friendsView; 
    ImageView image; 
    TextView username; 
    for(User user : users){ 
     friendsView = (RelativeLayout) View.inflate(ctx, R.layout.likes_view_friends, null); 
     image = (ImageView) friendsView.findViewById(R.id.image); 
     if(user.getImage() != null){ 
      image.setImageBitmap(user.getImage()); 
     } 
     username = (TextView) friendsView.findViewById(R.id.username); 
     username.setTypeface(tf); 
     String firstLetterLastName = user.getLastName().substring(0,1); 
     StringBuilder dotLastName = new StringBuilder(firstLetterLastName).append(".");   
     username.setText(user.getFirstName() + " " + dotLastName.toString()); 
     wrapper.addView(friendsView); 
    } 

Répondre

0

Envisagez d'utiliser GridView. Un tutoriel est disponible sur Android Developer Resources Hello Views

+0

J'ai oublié de dire que je fais cela dans une liste. Puis-je mettre le GridView dans le ListView ?! – Carnal

+0

Oui. Cependant, vous devrez gérer 2 adaptateurs. Un pour 'ListView' et un pour' GridView'. – Rajesh

+0

Je vois ce que vous dites. Mais je veux que GridView agisse comme un élément dans ListView, donc quand je fais défiler le ListView les images devraient aussi défiler avec le ListView. Votre suggestion me conduira à deux scrollviews, un pour listview et le second pour le gridview. – Carnal

Questions connexes