2016-09-19 7 views
0

J'essaye de créer un 15 (largeur) x20 (taille) «semblable à un« papier graphique »vue en utilisant GridLayout tel que défini comme tel sur xml.GridLayout attacher un ImageView dynamiquement

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:columnCount="15" 
        android:orientation="vertical" 
        android:rowCount="20" 
        android:layout_margin="2dp" 
        android:id="@+id/gridLayout_mazeLayout"> 
        <View 
         android:layout_width="15dp" 
         android:layout_height="15dp" 
         android:layout_row="0" 
         android:layout_column="0" 
         android:background="#51ffff" 
         android:layout_margin="1dp"/> 
        <View 
         android:layout_width="15dp" 
         android:layout_height="15dp" 
         android:layout_row="0" 
         android:layout_column="1" 
         android:background="#51ffff" 
         android:layout_margin="1dp"/> 
        <View 
         android:layout_width="15dp" 
         android:layout_height="15dp" 
         android:layout_row="0" 
         android:layout_column="2" 
         android:background="#51ffff" 
         android:layout_margin="1dp"/> 
    // rest of rows and cols not shown. 
    </GridLayout> 

Je voudrais joindre un ImageView à étendre sur les cellules 3x3

gridLayout_mazeLayout = (GridLayout) rootView.findViewById(R.id.gridLayout_mazeLayout); 
    ImageView imageView_robot = new ImageView(rootView.getContext()); 
    imageView_robot.setBackgroundResource(R.mipmap.imageacross3x3); 

    GridLayout.Spec row = GridLayout.spec(0); 
    GridLayout.Spec col = GridLayout.spec(0); 
    GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row, col); 
    gridLayout_mazeLayout.addView(imageView_robot,gridLayoutParam); 

Mais ce faisant, semble donc d'introduire un nouvel espace pour la ImagineView. Ce que je veux accomplir est d'attacher le ImagineView au-dessus de la cellule, au lieu de l'ajouter au GridLayout.

Répondre

0

changement <View> à l'intérieur <GridLayout> à <ImageView> avec Id unique et vous pouvez obtenir par exemple dans votre code Java en utilisant findViewById() ou si vous voulez construire dynamiquement simplement supprimer <View> de <GridLayout> étiquette et utiliser addView() pour insérer ImageView dans GridLayout.