簡體   English   中英

如何在循環中使用ViewGroup引用?

[英]How to use ViewGroup references in a loop?

對於主要活動的布局,我有以下代碼:

<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個按鈕,每個按鈕都有一個唯一的名稱。我的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. 與其單獨初始化每個按鈕,不如對3x3數組使用嵌套的for循環。(也就是說,我想對任何大小的網格通用化代碼)如何執行此操作?
  2. 在LinearLayout中,eclipse警告我,使用嵌套權重會降低性能。 有什么替代方法?

我建議您像下面這樣編輯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等。

對於第一個問題,您可以執行以下操作:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM