2017-05-08 49 views
0

我想在我要创建3 * 3 matrix.I创建了一个矩阵一个简单的数独游戏一个3×3矩阵网格,但我得到的问题是如何在特定位置中插入一些值网格布局。任何人都可以帮助我。如何创建Android的

到目前为止,我已经做了这个......

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mScrollView = (ScrollView) findViewById(R.id.scroll_view); 
    mScrollView.setSmoothScrollingEnabled(true); 

    mGrid = (GridLayout) findViewById(R.id.grid_layout); 
    // mGrid.setOnDragListener(new DragListener()); 

    final LayoutInflater inflater = LayoutInflater.from(this); 
    for (int i = 0; i < NBR_ITEMS; i++) { 
     final View itemView = inflater.inflate(R.layout.item_grid, mGrid, false); 
     final TextView text = (TextView) itemView.findViewById(R.id.text); 
     if ((i == 4)) { 

      text.setText("3"); 

     } 
     if ((i == 2)) { 

      text.setText("2"); 

     } 
     if ((i == 7)) { 

      text.setText("1"); 

     } 


     final int size = mGrid.getChildCount(); 
     System.out.println("size===" + size); 


     itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       final int index = calculateNewIndex(itemView.getX(), itemView.getY()); 
       System.out.println("index====" + index); 


       System.out.println("click on cell"); 
      } 
     }); 

     // itemView.setOnLongClickListener(new LongPressListener()); 
     mGrid.addView(itemView); 
    } 
} 
+0

使用表布局 –

+0

你自己做了什么? –

+0

@Bartek Lipinski我创建了一个使用网格布局和卡片视图的3 * 3矩阵。但事情是我想插入值在特定的位置。 –

回答

0

只要您使用的LinearLayout可以创建3×3的矩阵,做你想要wahtever这样,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".33" 
     android:orientation="horizontal" 
     android:weightSum="1" 
     > 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="1" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="2" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="3" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".33" 
     android:orientation="horizontal" 
     android:weightSum="1" 
     > 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="4" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="5" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="6" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".33" 
     android:orientation="horizontal" 
     android:weightSum="1" 
     > 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="7" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="8" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".33" 
      android:text="9" 
      android:gravity="center" 
      android:textSize="24sp" 
      /> 

    </LinearLayout> 

</LinearLayout> 

Like This

+0

@Nivesh感谢您的回答,但事情是我必须使用网格布局。我正在实施数独游戏。 –

+0

是的,我知道,但这是简单的,然后网格布局,你可以保持所有的屏幕尺寸与这一个layout.imply设置textView的背景或文本。 – 2017-05-08 09:40:35