简体   繁体   中英

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:

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);

    }
}
  1. Instead of initialization each button separately, I want to do so using a nested for loop for the 3x3 array.(That is I want to generalize the code for any sized grid) How do I do this?
  2. In the LinearLayout, eclipse warns me that using nested weights is bad performance-wise. What is the alternative to this?

i recommend you to edit your xml like below:

 <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..

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM