Merhaba Ahmet,
Sana temel olarak 2x2 seklinde dinamik buton nasıl oluşturulcagını örneklicem.Bundan sonra yapman gereken row saysını 2 carpmaktır.
public class MainActivity extends ActionBarActivity {
private static final int NUM_ROWS =2;
private static final int NUM_COLS =2 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createButton();
}
private void createButton(){
TableLayout table = (TableLayout) findViewById(R.id.tablelayout);
Button buttons[][] = new Button[NUM_ROWS][NUM_COLS];
for (int row = 0; row != NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this); tableRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f)); table.addView(tableRow);
for (int col = 0; col != NUM_COLS; col++){
final int FINAL_COL = col; final int FINAL_ROW = row;
Button button = new Button(this); button.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f));
button.setText("" + col + "," + row);
button.setPadding(0, 0, 0, 0);
tableRow.addView(button); buttons[row][col] = button;
}
}
}
}
xml kodları
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</TableLayout>
</RelativeLayout>