package com.hanqi.zuoyee; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class wanggeActivity extends AppCompatActivity { GridView gridView; List<nei> list =new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gridView=(GridView)findViewById(R.id.gv); setContentView(R.layout.activity_wangge); list.add(new nei(R.drawable.a,"内容")); list.add(new nei(R.drawable.a,"内容")); list.add(new nei(R.drawable.a,"内容")); list.add(new nei(R.drawable.a,"内容")); list.add(new nei(R.drawable.a, "内容")); list.add(new nei(R.drawable.a, "内容")); list.add(new nei(R.drawable.a, "内容")); base ba=new base(); gridView.setAdapter(ba); } class nei{ private int img; private String text; public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getText() { return text; } public void setText(String text) { this.text = text; } public nei(int img,String text){ this.img=img; this.text=text; } } class base extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { nei nei = list.get(position); if(convertView==null){ convertView=View.inflate(wanggeActivity.this,R.layout.wangge,null); } ImageView imageView=(ImageView)convertView.findViewById(R.id.iv); TextView textView=(TextView)convertView.findViewById(R.id.tv); imageView.setImageResource(nei.getImg()); textView.setText(nei.getText()); return convertView; } } }
java代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="70dp" android:layout_height="70dp" android:src="@drawable/a" android:id="@id/iv"/> <TextView android:layout_width="70dp" android:layout_height="wrap_content" android:gravity="center" android:text="介绍" android:id="@+id/tv"/> </LinearLayout> </LinearLayout>
样本视图代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hanqi.zuoyee.wanggeActivity"> <GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:id="@+id/gv"></GridView> </LinearLayout>
显示用的视图
时间: 2024-10-16 21:06:18