关于网格布局实现图标在上,文字在下的效果
先看效果图:
再来点实在的,上代码:
activity_main.xml
<?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"
android:orientation="vertical"
tools:context="com.example.myapplication1.MainActivity" >
<GridView
android:id="@+id/gridview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
items.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/logo" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>
实体类:MyBean.java
public class MyBean {
private String iconName;
private int imageId;
public MyBean(String iconName, int imageId) {
super();
this.iconName = iconName;
this.imageId = imageId;
}
public String getIconName() {
return iconName;
}
public void setIconName(String iconName) {
this.iconName = iconName;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
适配器MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater = null;
private Context mContext;
private ArrayList<MyBean> mArrayList = null;
public MyAdapter(Context context, ArrayList<MyBean> arrayList) {
mContext = context;
mArrayList = arrayList;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mArrayList == null ? 0 : mArrayList.size();
}
@Override
public Object getItem(int position) {
return mArrayList == null ? null : mArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
MyBean mBean = (MyBean) getItem(position);
if (convertView == null) {
convertView = mInflater.from(mContext).inflate(R.layout.items, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
holder.textView = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource(mBean.getImageId());
holder.textView.setText(mBean.getIconName());
return convertView;
}
class ViewHolder {
ImageView imageView;
TextView textView;
}
}
主活动MainActivity:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import com.example.myapplication1.MyAdapter;
public class MainActivity extends AppCompatActivity implements /*View.OnClickListener,*/ AdapterView.OnItemClickListener {
private GridView mGridView;
private ArrayList<MyBean> mArrayList;
private Button mBtnSelectShow;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.gridview1);
initdata();
adapter = new MyAdapter(this,mArrayList);
setLayout();
}
/**
* 添加数据
*/
private void initdata() {
mArrayList = new ArrayList<MyBean>();
mArrayList.add(new MyBean("我的积分", R.mipmap.score));
mArrayList.add(new MyBean("积分排名", R.mipmap.rank64x64));
mArrayList.add(new MyBean("积分奖扣", R.mipmap.addscore64x64));
mArrayList.add(new MyBean("我的审核", R.mipmap.review64x64));
mArrayList.add(new MyBean("我的奖票", R.mipmap.gift64x64));
mArrayList.add(new MyBean("奖扣详情", R.mipmap.detail64x64));
mArrayList.add(new MyBean("任务闹钟", R.mipmap.schedule64x64));
mArrayList.add(new MyBean("帮助中心", R.mipmap.help64x64));
}
private void setLayout() {
mGridView = (GridView) findViewById(R.id.gridview1);
mGridView.setVisibility(View.VISIBLE);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(this);
mGridView.setSelection(0);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.gridview1 :
Toast.makeText(MainActivity.this,"gridView" + mArrayList.get(position).getIconName(),Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
版权声明:本文为jiangzuomeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。