问题
I am working on a Suduko solver app. I want the layout to have 9x9 TextView in 9 Gridviews. I want to put below them two buttons (Solve, clear). I have made it but it does not work probably with small devices (Can`t see the buttons and the Grid view turned to scrollable that does not fit the screen)
Here is how I add Textviews to the 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));
Here is my 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>
And this is a screenshot of what I had

But this is what I need my Layout to be with all Devices. Maybe I should use weights, but I don`t know how

回答1:
Here's a trick: For a completely uniform table or grid, you don't need TableView or GridView at all. Just put everything inside LinearLayout with size=0 and layoutWeight=1. That size=0 seems counter-intuitive, but combined with the layoutWeight=1, it has the effect of giving all child widgets the exact same size, regardless of their contents.
So do something like this:
<!-- 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>
回答2:
Check this Sudoku project -
Git Hub
Also
Check This
Here you can find layout and source code for Sudoku and also other useful things.
来源:https://stackoverflow.com/questions/16389500/how-to-use-weights-to-arrange-gridview-items-programmatically-with-tablelayout