public class ActivityOne extends Incompatibility {
Context context;
Base Adapter Base Adapter;
int[] ou = {R.draw able.accounting, R.draw able.accounted, R.draw able.accounted, R.draw able.unaccountably, R.draw able.show info, R.draw able.asset, R.draw able.account flag, R.draw able.help, R.draw able.exit};
String[] strings = {"新增支出", "新增收入", "我的支出", "我的收入", "数据管理", "系统设置", "收支便签", "帮助", "退出"};
private GridView gridview;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zctivity);
initView();
context = this;
Base Adapter = new Base Adapter() {
private void initView(View view) {
image = (ImageView) view.findViewById(R.id.image);
text = (TextView) view.findViewById(R.id.text);
}
private TextView text;
private ImageView image;
@Override
public int getCount() {
return strings.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview, null);
initView(convertView);
text.setText(strings[position]);
image.setImageResource(ou[position]);
return convertView;
}
};
gridview.setAdapter(Base Adapter);
}
private void initView() {
gridview = (GridView) findViewById(R.id.gridview);
}
}
②public class ActivityOne extends AppCompatActivity {
Context context;
BaseAdapter baseAdapter;
int[] tou = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8};
String[] strings = {"吃饭", "晚安", "爱心", "篮球", "小猪", "鲜花", "西瓜", "乒乓球"};
private GridView gridview;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zctivity);
initView();
context = this;
baseAdapter = new BaseAdapter() {
private void initView(View view) {
image = (ImageView) view.findViewById(R.id.image);
text = (TextView) view.findViewById(R.id.text);
}
private TextView text;
private ImageView image;
@Override
public int getCount() {
return strings.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.gridview, null);
initView(convertView);
text.setText(strings[position]);
image.setImageResource(tou[position]);
return convertView;
}
};
gridview.setAdapter(baseAdapter);
}
private void initView() {
gridview = (GridView) findViewById(R.id.gridview);
}
}
2、Xml
①<?xml version="1.0" encoding="utf-8"?>
<GridView
android:id="@+id/gridview"
android:numColumns="3"
android:padding="20dp"
android:horizontalSpacing="30dp"
android:verticalSpacing="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"></GridView>
②<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:src="@drawable/top1"
android:layout_width="match_parent"
android:layout_height="400dp"></ImageView>
<GridView
android:id="@+id/gridview"
android:numColumns="4"
android:padding="20dp"
android:horizontalSpacing="30dp"
android:verticalSpacing="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"></GridView>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190415112811764.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE3NDUzNg==,size_16,color_FFFFFF,t_70)