理想情况下,可以通过 findViewById 方法检索视图引用(在 xml 文件中)。但是我有一个类,我已经实例化了所有视图
public View Initialise(Context context){
private Button mBoardButtons[][] = new Button[gridSize][gridSize];
private TableRow rowArr[] = new TableRow[gridSize];
private TextView mInfoTextView;
private TextView mPlayer;
private TextView mPlayerCount;
TableLayout tableLayout1 = new TableLayout(context);
TableLayout.LayoutParams tableParams1 = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tableParams1.topMargin = 0;
tableLayout1.setLayoutParams(tableParams1);
for ( int i =0 ; i < gridSize ; i++){
rowArr[i] = new TableRow(context);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rowParams.gravity=Gravity.CENTER_HORIZONTAL;
rowArr[i].setLayoutParams(rowParams);
for(int j = 0; j < gridSize ; j++){
mBoardButtons[i][j] = new Button(context);
mBoardButtons[i][j].setText(Integer.toString(i)+","+Integer.toString(j));
mBoardButtons[i][j].setTextSize(150/gridSize);
mBoardButtons[i][j].setMaxHeight(450/gridSize);
mBoardButtons[i][j].setMaxWidth(600/gridSize);
rowArr[i].addView(mBoardButtons[i][j]);
}
tableLayout1.addView(rowArr[i]);
}
mInfoTextView = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = 5;
params.gravity=Gravity.CENTER;
mInfoTextView.setLayoutParams(params);
mInfoTextView.setText("Info");
mInfoTextView.setTextSize(25);
tableLayout1.addView(mInfoTextView);
TableLayout tableLayout2 = new TableLayout(context);
TableLayout.LayoutParams tableParams2 = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tableLayout2.setLayoutParams(tableParams2);
TableRow tableRow = new TableRow(context);
mPlayer = new TextView(context);
mPlayer.setText("Player: ");
tableRow.addView(mPlayer);
mPlayerCount = new TextView(context);
mPlayerCount.setText(" ");
tableRow.addView(mPlayerCount);
return tableLayout1;
}
上面的代码是在一个特定的类中定义的(可以说它是一个 xml 文件的替代品)
现在我如何在我的 Activity 类中获取所有这些 View 元素的引用。
是否有任何类似于 findViewById 的方法来检索以编程方式定义的视图引用?