类似手机系统桌面的九宫格布局,以图片和文字进行搭配,可以使用GridView来实现。类似效果如下:![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6811a6647f3a9b33976c86e5dd794452.png)
使用GridView实现过程:
1.在activity_main.xml中修改代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/channel"
android:numColumns="3"
android:verticalSpacing="24dp"
android:layout_marginTop="40dp"
>
</GridView>
</LinearLayout>
关于GridView的详细属性:https://blog.csdn.net/lyy666888/article/details/79163632
2.创建Channel类用来表示每个单元格的内容
public class Channel {
private int imgId;
private String dec;
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public String getDec() {
return dec;
}
public void setDec(String dec) {
this.dec = dec;
}
}
3.创建ChannelAdapter类继承BaseAdapter来作为GridView的适配器
public class ChannelAdapter extends BaseAdapter {
private ArrayList<Channel> channelList;
private LayoutInflater layoutInflater;
public ChannelAdapter(ArrayList<Channel> list, Context context){
channelList = list;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return channelList<