Implementation of horizontal and vertical scrolling of gridview in Android (dynamic)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.