After my experiments, we can achieve horizontal and vertical scrolling perfectly. Let's not talk much about it. First, let's look at the layout file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <HorizontalScrollView android:layout_width="match_parent" android:layout_height="fill_parent" > <FrameLayout android:layout_width="fill_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout_gridtableLayout" android:layout_width="300dp" android:layout_height="match_parent" android:orientation="horizontal" > <GridView android:id="@+id/tablegrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="#ffffff" android:columnWidth="100dp" android:gravity="center" android:horizontalSpacing="1dp" android:scrollbarAlwaysDrawHorizontalTrack="true" android:scrollbarAlwaysDrawVerticalTrack="true" android:scrollbars="horizontal|vertical" android:verticalSpacing="1dp" /> </LinearLayout> </FrameLayout> </HorizontalScrollView></LinearLayout>
Specify the width of linearlayout to change the length and width of your gridview. If its length exceeds the screen, a horizontal scroll bar is automatically added.
However, if you want to dynamically specify the width of your gridview in the program, the sample code is as follows:
Linearlayout ll_gridetablelayout = (linearlayout) tableview. findviewbyid (R. id. linearlayout_gridtablelayout); ll_gridetablelayout.setlayoutparams (New framelayout. layoutparams (// dynamically set the width of 100 * coloumnnum, linearlayout. layoutparams. match_parent ));
Note that, although we want to modify the width of linearlayout, we cannot use linearlayout. lyoutparam is used as its setlayoutparms parameter, and its parent must be used, that is, the layoutparam of framelayout. Otherwise, your program will report an exception.