2013-05-05 90 views
1

我正在研究Suduko求解器應用程序。我希望佈局在9個Gridviews中有9x9的TextView。我想在它們下面放兩個按鈕(Solve,clear)。我做了它,但它不與小型設備可能工作(不能老是看到的按鈕和網格視圖轉向滾動不適合屏幕)如何使用權重以編程方式使用TableLayout排列Gridview項目

這是我如何添加Textviews到GridView

gridView = (GridView) findViewById(R.id.gridView1); 
String[] arrayEmpty = new String[] {"", "", "", "", "", "", "", "", ""}; 
ArrayList<String> listEmpty = new ArrayList<String>(Arrays.asList(arrayEmpty)); 
gridView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,list)); 

這裏是我的Layout.XML

<?xml version="1.0" encoding="utf-8"?> 

<TableRow android:id="@+id/tableRow1" 
    android:layout_width="match_parent" 
    android:background="#999999" 
    android:layout_weight="1" 
    android:layout_height="match_parent"> 

    <GridView 
    android:id="@+id/gridView1" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
    </GridView> 

    <GridView 
    android:id="@+id/gridView2" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<GridView 
    android:id="@+id/gridView3" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<TableRow android:id="@+id/tableRow2" 
    android:layout_width="match_parent" 
    android:background="#999999" 
    android:layout_weight="1" 
    android:layout_height="match_parent"> 

    <GridView 
    android:id="@+id/gridView4" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
    </GridView> 

    <GridView 
    android:id="@+id/gridView5" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<GridView 
    android:id="@+id/gridView6" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<TableRow android:id="@+id/tableRow3" 
    android:layout_width="match_parent" 
    android:background="#999999" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:layout_marginBottom="15dp"> 

    <GridView 
    android:id="@+id/gridView7" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
    </GridView> 

    <GridView 
    android:id="@+id/gridView8" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<GridView 
    android:id="@+id/gridView9" 
    android:clickable="true" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="2dp" 
    android:background="#5C5C5C" 
    android:layout_weight="1" 
    android:numColumns="3" > 
</GridView> 

<TableRow android:layout_weight="1" android:layout_height="match_parent"> 
    <Button 
     android:id="@+id/solve_button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:text="@string/solve" /> 
    <Button 
     android:id="@+id/clear_button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:text="@string/_clear" /> 
</TableRow> 

這是什麼,我有enter image description here

截圖但是,這是我需要我的佈局要與所有設備。也許我應該使用權,但我不`噸知道如何

enter image description here

回答

2

這裏有一個技巧:對於一個完全統一的表或網格,您根本不需要TableView或GridView。將所有內容放在LinearLayout中,大小= 0,layoutWeight = 1。這個大小= 0似乎違反直覺,但與layoutWeight = 1相結合,它的作用是給予所有子部件完全相同的大小,而不管它們的內容如何。

所以做這樣的事情:

<!-- Outer container; a vertical layout that fills the screen --> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <!-- first row --> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    <!-- contents of the first row --> 
    <!-- first item in first row --> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
    <!-- second item in first row --> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
    <!-- third item in first row --> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
    </LinearLayout> 
    <!-- second row --> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    <!-- contents of the second row. --> 
    <!-- etc --> 
    </LinearLayout> 
    <!-- third row --> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 
    <!-- contents of the third row --> 
    <!-- etc --> 
    </LinearLayout> 
    <Button android:id="@+id/solveButton" 
     android:layout_width="fill_parent" 
     android:layout_width="wrap_content" /> 
    <Button android:id="@+id/clearButton" 
     android:layout_width="fill_parent" 
     android:layout_width="wrap_content" /> 
</LinearLayout> 
+0

注:我已經離開它作爲一個練習爲學生找出如何把利潤率您想要的地方。 – 2013-05-06 06:52:46

+0

好吧,但我應該如何將TextView添加到.java文件(Progammatically)的每個視圖? – 2013-05-06 06:55:05

+1

LinearLayout不處理緩存,如果您在其中放置太多視圖足以耗盡內存,應用程序將崩潰。但在gridview或listview中,這不會發生。 – 2017-03-03 10:48:36

2

檢查這個數獨的項目 -

Git Hub

而且

Check This

在這裏你可以找到佈局和Sudoku的源代碼以及其他有用的東西。

相關問題