
上QQ阅读APP看书,第一时间看更新
How to do it...
- Starting with the TableLayout project, open activity_main.xml. Change the root layout to TableLayout.
- Add three TableRow objects with three sets of TextView objects to each TableRow to create a 3 x 3 matrix. For demonstration purposes, the columns are labeled A-C and the rows 1-3, so the first row of TextView objects will be A1, B1, and C1. The final result will look like this:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A1" android:id="@+id/textView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B1" android:id="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C1" android:id="@+id/textView3" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A2" android:id="@+id/textView4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B2" android:id="@+id/textView5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C2" android:id="@+id/textView6" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A3" android:id="@+id/textView7" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B3" android:id="@+id/textView8" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C3" android:id="@+id/textView9" /> </TableRow> </TableLayout>
- Now, open the GridLayout project to edit activity_main.xml. Change the root layout to GridLayout. Add the columnCount=3 and rowCount=3 attributes to the GridLayout element.
- Now, add nine TextView objects to GridLayout. We will use the same text as the preceding TableLayout for a consistent comparison. Since GridView does not use TableRow objects, the first three TextView objects are in row 1, the next three are in row 2, and so on. The final result will look like this:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="3" android:rowCount="3"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A1" android:id="@+id/textView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B1" android:id="@+id/textView2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C1" android:id="@+id/textView3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A2" android:id="@+id/textView4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B2" android:id="@+id/textView5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C2" android:id="@+id/textView6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A3" android:id="@+id/textView7" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B3" android:id="@+id/textView8" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C3" android:id="@+id/textView9" /> </GridLayout>
- You can either run the application or use the Design tab to see the results.