Java 如何在循环中使用视图组引用?
我的主要活动布局有以下代码:Java 如何在循环中使用视图组引用?,java,android,android-layout,android-linearlayout,Java,Android,Android Layout,Android Linearlayout,我的主要活动布局有以下代码: <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:co
<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>
我建议您按如下方式编辑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>
你可以根据你的布局改变按钮的高度。布局大、布局小、布局大等等。对于第一个问题,您可以这样做:
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]);
int[]ids=newint[]{
{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};
对于(int i=0;i<3;i++)
对于(int j=0;j<3;j++)
g[i][j]=(按钮)findViewById(id[i][j]);
您有一个代码错误。我想你想要的是g[1][2]
而不是g[1][0]
。不要担心2。(另一种选择是尽可能使用相对布局)但真的不用担心2..哦,谢谢,更正了。我问了第二个问题,因为上面的代码在我的模拟器中不起作用。应用程序只是在启动时崩溃,只有当我使用嵌套权重时才会发生这种情况。我很抱歉,但也许我在最初的问题中不清楚:我想要的是避免为每个按钮分配一个值。我希望这是通用的(比如NxN网格),就像在一个循环中一样。
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]);