GridView和ScrollView~Android基础(三)

GridView和ScrollView的使用

课程

GridView

(1)GridView的用法和ListView类似声明控件,绑定控件id

GridView mGridviewbutton = findViewById(R.id.grid_view_button);

设置Base Adapter适配器,设置适配器一样有两种方法,匿名内部类和新建类继承BaseAdapter

class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
	    //有多少条数据
        return 0;
    }

    @Override
    public Object getItem(int i) {
	    //返回某一条数据对象
        return null;
    }

    @Override
    public long getItemId(int i) {
    	//返回某个位置
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
    	 //返回一个视图
        return null;
    }
}

(2)GridView与ListView不同之处,GridView可以多列显示数据

<!--设置每一列的宽度-->
android:columnWidth="100dp"
<!--设置每一列的水平距离-->
android:horizontalSpacing="30dp"
<!--设置每一列的垂直距离-->
android:verticalSpacing="100dp"
<!--要显示的列数-->
android:numColumns="auto_fit" 

ScrollView

在ScrollView中只能有一个同层次对象,如下面的代码中,只能写一个Linearlayout,写两个就会显示内容

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="我666" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="66666" />
        </LinearLayout>
    </ScrollView>

这样子同层次的只有一个就可以显示内容

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值