2013-07-29 5 views
6

tic-tac-toeゲーム(アンドロイド版)を作ろうとしています。 デバイスの幅と高さに関して、9つのボタンすべてを自動サイズ調整し、3 * 3グリッドに均等に配置したいと考えています。しかし、今は自分のサイズの番号しか設定できません。 親の高さと幅を使用してサイズを計算させる方法を教えてもらえますか?アンドロイドのxmlレイアウトで3つの行に9つのボタンを配置する方法は?

また、今はグリッドレイアウトを使用しています。これはチック・タック・トゥ・ゲームに使うべきレイアウトですか?

ありがとうございました。

+2

あなたは、私がこれを試してみましたが、私は私がしたい行数を指定する方法を知らないのGridView – midhunhk

答えて

8

使用LinearLayoutsandroid:weightSumandroid:layout_weight

を編集

例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="3" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="3" > 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

</LinearLayout> 
+0

あなたはそれらを使用する方法の例を示すことができますか? thx〜 – sy19890515

+0

@ sy19890515私の更新された答えを見てください! –

+0

ありがとう、とてもうまくいきます。しかし、私はなぜグリッドレイアウトでそれをやれないのだろうと思っています。私はrowcoundとcolumncountを3に指定し、0,0,0,1,0,2,1,0に9個のボタンを入れました。しかし、それらのスペースを均等に取ることはできません。あなたはなにか考えはありますか?ありがとうございました! – sy19890515

0

これを試してみてください:

<GridView 
    android:id="@+id/grid_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:horizontalSpacing="10dp" 
    android:numColumns="3" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="10dp" /> 
+0

を試すことができます。私はそれだけで列の数を伝えることができ、それは私にすべての方法を下に行くアイテムの束を与えた...と私もそれにボタンを置くことができます... – sy19890515

0

GridViewを使用できます。 ボタンを均等に配布します。あなたの活動の使用中

<GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:horizontalSpacing="5dp" 
    android:numColumns="3" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="5dp" />` 

このコード

のGridView GridViewの。

static final String[] numbers = new String[] { 
"A", "B", "C", 
    "D", "E", "F", 
    "G", "H", "I"}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 

gridView = (GridView) findViewById(R.id.gridview); 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, numbers); 

gridView.setAdapter(adapter); 

gridView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, 
     int position, long id) { 
     Toast.makeText(getApplicationContext(), 
     ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 
    } 
}); 


} 
+0

私はgridviewを使ってみましたが、それはちょうど私に一連のテキスト "item#(ここでは#は1からいくつかの数字です)"がすべて下がってきています。私はどのようにボタンをグリッドビューに置くのですか?最初にxmlファイルを作成したいと思います。ありがとう〜 – sy19890515

+0

あなたはカスタムアダプターを使用する必要があります。このリンクを確認してください http://www.mkyong.com/android/android-gridview-example/ – rachit

1
<LinearLayout 
android:weightSum = 1.0 
> 
    <LinearLayout 
    android:weightSum=1.0 
    android:layout_weight = 0.33 
    android:orientation = horizontal 
    > 
    <Button 
     android:layout_weight=0.33 
    /> 
    <Button 
     android:layout_weight=0.33 
    /> 
    <Button 
    android:layout_weight=0.33 
    /> 
</LinearLayout> 
    //Similar to above LinearLayout of Buttons add 2 more LinearLayouts 

1

私はまったく同じ問題に取り組んでおり、テーブルのレイアウトで動作するようにこれを得ています。お役に立てれば。

<TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/editText1" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:padding="40dp" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      style="@style/button_style" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" > 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      style="@style/button_style" /> 
    </TableRow> 
</TableLayout> 

`

関連する問題