Android中GridView水平滚动和垂直滚动的实现(动态)

经过本人实验,完美实现水平滚动和垂直滚动。话不多说,先看布局文件:

<?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>

指定其中LinearLayout的宽度就能够实现你GridView的长宽变化,如果它的长超过屏幕,则自动添加水平滚动条。

但是如果你还想在程序当中动态指定你的GridView的宽度,则示例代码如下:

LinearLayout ll_gridetableLayout=
				(LinearLayout)tableView.findViewById(R.id.linearLayout_gridtableLayout);
		ll_gridetableLayout.setLayoutParams(new FrameLayout.LayoutParams(//动态设置宽度
				100*coloumnNum,
				LinearLayout.LayoutParams.MATCH_PARENT));

这里要注意了,虽然我们要修改LinearLayout的宽度,但是我们却不能使用LinearLayout.LyoutParam来作为它setLayoutParms的参数,而必须使用它的parent,也就是FrameLayout的LayoutParam,否则你的程序是要报异常的。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值