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>