2016-09-19 10 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> 

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

しかし、そうすることにまたがるするImageViewを添付したいと思いますがImagineViewのための新たなスペースをご紹介しているようです。私が達成したいのは、GridLayoutに追加する代わりに、ImagineViewをセルの上に付けることです。ユニークなIDを持つ<ImageView><GridLayout>内部

答えて

0

変更<View>、その後、あなたはfindViewById()を使用して、Javaコードでインスタンスを取得することができたり、動的にそれを構築したい場合は、単に<GridLayout>タグから<View>を削除し、GridLayoutImageViewを挿入するためにaddView()を使用しています。

関連する問題