Android 如何检索以编程方式定义的视图的引用
理想情况下,可以通过findViewById方法检索视图引用(在xml文件中)。 然而,我有一个类,我已经实例化了所有视图Android 如何检索以编程方式定义的视图的引用,android,android-view,android-tablelayout,Android,Android View,Android Tablelayout,理想情况下,可以通过findViewById方法检索视图引用(在xml文件中)。 然而,我有一个类,我已经实例化了所有视图 public View Initialise(Context context){ private Button mBoardButtons[][] = new Button[gridSize][gridSize]; private TableRow rowArr[] = new TableRow[gridSize]; priv
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;
}
公共视图初始化(上下文){
私有按钮mBoardButtons[][]=新按钮[gridSize][gridSize];
private TableRow rowArr[]=新TableRow[gridSize];
私有文本视图mInfoTextView;
私有文本视图mPlayer;
私有文本视图mPlayerCount;
TableLayout tableLayout1=新的TableLayout(上下文);
TableLayout.LayoutParams tableParams1=新的TableLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
tableParams1.topMargin=0;
tableLayout1.setLayoutParams(tableParams1);
对于(int i=0;i
上面的代码是在一个特定的类中定义的(假设它是xml文件的替代品)
现在,我如何在活动类中检索所有这些视图元素的引用
有没有类似于findViewById的方法来检索编程定义的视图引用?当您使用新的视图引用创建视图引用时,您就拥有了该引用。把它保存在某个地方
或者您可以使用setID(),以便以后可以使用findViewByID。但是,如果您现在就可以保存它,那么这样做是愚蠢的。为什么不将引用与成员保留在同一个类中,这样您就可以在activity classIn xml文件中添加一些getter并访问这些视图呢?在我们膨胀的xml文件中,或者执行setContentView(R.layout.xmlfile)如何才能引用编程定义的视图元素?如果是以编程方式创建它们,那么在创建它们时就有了引用。把它们留着以后用吧。或者,您可以在每个id上调用setID,然后在该id上使用findViewByID。