[英]How to use ViewGroup references in a loop?
I have the following code for my main activity's layout: 对于主要活动的布局,我有以下代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/R0C0"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R0C1"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R0C2"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/R1C0"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R1C1"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R1C2"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/R2C0"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R2C1"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/R2C2"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
There are 9 buttons for a tic-tac-toe grid, each with a unique name.My mainActivity class is: 井字游戏网格有9个按钮,每个按钮都有一个唯一的名称。我的mainActivity类是:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends Activity {
Button g[][] = new Button[3][3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g[0][0]=(Button)findViewById(R.id.R0C0);
g[0][1]=(Button)findViewById(R.id.R0C1);
g[0][2]=(Button)findViewById(R.id.R0C2);
g[1][0]=(Button)findViewById(R.id.R1C0);
g[1][1]=(Button)findViewById(R.id.R1C1);
g[1][2]=(Button)findViewById(R.id.R1C2);
g[2][0]=(Button)findViewById(R.id.R2C0);
g[2][1]=(Button)findViewById(R.id.R2C1);
g[2][2]=(Button)findViewById(R.id.R2C2);
}
}
i recommend you to edit your xml like below: 我建议您像下面这样编辑xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/R0C0"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/R0C1"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp" />
<Button
android:id="@+id/R0C2"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/R0C3"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/R0C4"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp" />
<Button
android:id="@+id/R0C5"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/R0C6"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
<Button
android:id="@+id/R0C7"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp" />
<Button
android:id="@+id/R0C8"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_width="0dp"/>
</LinearLayout>
</LinearLayout>
you can change button height according to your layout...eg. 您可以根据自己的布局更改按钮的高度...例如 layout-large, layout-small, layout-xlarge etc..
布局大,布局小,布局xlarge等。
For your first question, you can do this: 对于第一个问题,您可以执行以下操作:
int[][] ids = new int[][]{
{R.id.R0C0, R.id.R0C1, R.id.R0C2},
{R.id.R1C0, R.id.R1C1, R.id.R1C2},
{R.id.R2C0, R.id.R2C1, R.id.R2C2}};
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
g[i][j] = (Button) findViewById(ids[i][j]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.