![](/img/trans.png)
[英]How I can use the OnClickListener for RelativeLayout 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);
}
}
我建議您像下面這樣編輯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.