2016-09-19 3 views
0

xml에 정의 된대로 GridLayout을 사용하여 15 (너비) x20 (높이) "그래프 - 용지"보기를 생성하려고합니다.GridLayout 동적으로 ImageView를 연결하십시오

<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> 

I는 3 × 3 셀

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); 

그러나 그렇게 걸쳐하는 ImageView을 첨부하고 싶은는 ImagineView을위한 새로운 공간을 소개하는 것 같다. 달성하고자하는 것은 GridLayout에 추가하는 대신 ImagineView을 셀의 맨 위에 첨부하는 것입니다.

답변

0

고유 한 ID와 <ImageView><GridLayout> 내부 변경 <View> 다음은 findViewById()를 사용하여 자바 코드에서 인스턴스를 얻을 수 있습니다 또는 동적으로 구축하려는 경우 단지 <GridLayout> 태그에서 <View>를 제거하고 GridLayoutImageView를 삽입 addView()를 사용합니다.