2015-04-07 16 views
0

我已經爲我的主要活動的佈局下面的代碼:如何在循環中使用ViewGroup引用?

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

有9個按鍵的井字棋格,每一個獨特的name.My 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); 

    } 
} 
  1. 代替初始化的每個按鈕單獨,我想這樣做使用嵌套for循環的3×3陣列(即我要概括的代碼爲任何尺寸的網格)如何做 這個?
  2. 在LinearLayout中,eclipse警告我使用嵌套權重在性能方面表現不佳。這有什麼替代?
+0

你有一個代碼錯誤。我想你想'g [1] [2]'而不是'g [1] [0]''。 –

+0

不用擔心2.(替代方案是儘可能使用相對佈局)但真的不用擔心2 .. –

+0

哦謝謝,糾正。我問了第二個問題,因爲上面的代碼不在我的模擬器中工作。應用程序在啓動時崩潰,而這種情況只發生在我使用嵌套權重時發生。 – 343GuiltySpark

回答

0

我建議你編輯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> 

你可以根據你的佈局改變按鈕的高度...如。佈局大,佈局小,佈局XLARGE等。

0

關於第一個問題,你可以這樣做:

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

我的歉意,但也許我不清楚我的原始問題:我想要的是避免分配每個單獨的按鈕的值。我想這是泛化的(就像NxN網格一樣),就像循環一樣。 – 343GuiltySpark