Android studio APP开发 GridView 和 ScrollView

GridView and ScrollView

GridView

GridView ScrollView和 ListView几乎是一样的,创建过程也和ListView一样,可以参照这篇博文:https://blog.csdn.net/Ace_bb/article/details/104066710

与List‘View的不同之处在于布局文件中的一些属性不同。
先来看下之前创建的ListView的界面布局:
list'view

属性一 numColumns

在布局文件中加入下面代码:

android:numColumns="3"

界面效果如图:
3列
通过修改numColumns后面的数字可以修改控件展示的列数。
这个功能可以用于相册,QQ微信的表情包列表等

numColumns 也可以设置成自适应,系统会根据控件的大小自己设定列数。

        android:numColumns="auto_fit"

也可以自己设定控件的宽度,来让系统自适应列数,代码如下:

        android:columnWidth="50dp"

属性二 设计列表中控件之间的距离

可以设置列表中控件于控件之间的距离,代码如下:

        android:horizontalSpacing="110dp"
        android:verticalSpacing="110dp"

设置之后效果如图:
效果图

ScrollView

ScrollView用于不是列表的内容区滚动。 只支持垂直滚动,scrollView控件里面也只能放一个控件,但是可以加入一个LinearLayout,在往LinearLayout里面加入其他控件。
这里直接上代码:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/grid_view_button">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我是来自于ScrollView中的Button"/>

        </LinearLayout>

    </ScrollView>

效果如图:
视图
盖在这个布局上面,支持滚动。

发布了50 篇原创文章 · 获赞 3 · 访问量 5179

猜你喜欢

转载自blog.csdn.net/Ace_bb/article/details/104068218