2013-07-29 15 views
6

Tôi đang cố gắng tạo trò chơi tic-tac-toe (phiên bản Android). Tôi muốn làm cho tất cả 9 nút tự động định cỡ liên quan đến chiều rộng và chiều cao của thiết bị và đặt chúng đồng đều trong lưới 3 * 3. Nhưng tôi chỉ có thể đặt số cho kích thước của chúng ngay bây giờ. Bất cứ ai có thể cho tôi biết cách để họ sử dụng chiều cao và chiều rộng của phụ huynh và tính toán kích thước của họ?cách đặt 9 nút trong 3 hàng trong bố cục xml android?

Ngoài ra, hiện tại, tôi đang sử dụng bố cục lưới. Đây có phải là bố cục tốt nhất tôi nên sử dụng cho trò chơi tic-tac-toe không?

Cảm ơn.

+2

Bạn có thể thử GridView – midhunhk

Trả lời

8

Sử dụng LinearLayouts với android:weightSumandroid:layout_weight

Sửa

Ví dụ:

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

bạn có thể cho thấy một ví dụ về cách sử dụng chúng? thx ~ – sy19890515

+0

@ sy19890515 xem câu trả lời cập nhật của tôi! –

+0

Cảm ơn rất nhiều, hoạt động rất tốt. Nhưng Im cũng tự hỏi tại sao tôi không thể làm điều đó với gridlayout. Tôi đã chỉ định mã hàng và cột là 3 và đặt 9 nút ở 0,0, 0,1, 0,2, 1,0, ... Nhưng tôi không thể để chúng mất không gian đồng đều. Bạn có bất cứ ý tưởng? cám ơn bạn một lần nữa! – sy19890515

0

Hãy thử điều này:

<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

tôi đã cố gắng này, nhưng tôi không biết làm thế nào để xác định số lượng hàng tôi muốn. Tôi chỉ có thể nói với nó số lượng cột và nó đã cho tôi một loạt các mục đi tất cả các con đường xuống ... và tôi có thể đặt nút trên nó là tốt ... – sy19890515

0

Bạn có thể sử dụng GridView. Nó sẽ phân phối các nút đồng đều.

<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" />` 

trong việc bạn sử dụng hoạt động mã này

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

Tôi đã thử sử dụng GridView nhưng nó chỉ cho tôi một loạt các văn bản "mụC# (trong đó # là từ 1 đến một số số)" đi tất cả các con đường xuống. làm thế nào để đặt các nút trên GridView và làm thế nào để nói nó chỉ cho tôi 9 nút? và tôi muốn làm việc trên tệp xml trước tiên. cảm ơn ~ – sy19890515

+0

cho rằng bạn sẽ phải sử dụng bộ điều hợp tùy chỉnh. kiểm tra liên kết này 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

tôi đang làm việc trên cùng một vấn đề chính xác và nhận được điều này để làm việc với bố trí bảng. Hi vọng điêu nay co ich.

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

'