GridView显示ICON和TEXT
1.设置GridView的布局
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" />
2.设置Icon+Text的布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_gravity="center" > <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_gravity="center"> <ImageView android:id="@+id/icon_image" android:layout_height="80dip" android:layout_width="80dip" android:gravity="center" android:layout_gravity="center" > </ImageView> <TextView android:id="@+id/icon_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:gravity="center" android:layout_gravity="center" android:textSize="13px" android:layout_marginTop="4dip" > </TextView> </LinearLayout> </FrameLayout>
3.创建显示的Adapter
public class HomeImageAdapter extends BaseAdapter { private Context mContext; private LayoutInflater mInflater; LinearLayout.LayoutParams params; private Integer[] mThumbIds = { R.drawable.s81, R.drawable.s82, R.drawable.s84, R.drawable.s87, R.drawable.s94 }; private String[] mStrings = {"联系电话","国家","省","城市","城市II"}; public HomeImageAdapter(Context c){ mContext = c; mInflater = LayoutInflater.from(c); params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ItemViewTag viewTag; if (convertView == null) { convertView = mInflater.inflate(R.layout.home_icontext, null); // construct an item tag viewTag = new ItemViewTag((ImageView) convertView.findViewById(R.id.icon_image), (TextView) convertView.findViewById(R.id.icon_text)); convertView.setTag(viewTag); } else { viewTag = (ItemViewTag) convertView.getTag(); } // set name viewTag.mName.setText(mStrings[position]); // set icon viewTag.mIcon.setBackgroundResource(mThumbIds[position]); viewTag.mIcon.setLayoutParams(params); return convertView; } class ItemViewTag { protected ImageView mIcon; protected TextView mName; /** * The constructor to construct a navigation view tag * * @param name * the name view of the item * @param size * the size view of the item * @param icon * the icon view of the item */ public ItemViewTag(ImageView icon, TextView name) { this.mName = name; this.mIcon = icon; } } }
相关推荐
chenjinlong 2020-01-29
vickyjfr 2013-08-27
luoj 2011-08-17
huohu00 2015-04-22
omguare 2012-05-07
SuperMenyII 2019-06-26
AngelicaA 2011-05-02
AABBbaby 2018-07-27
DrZOU 2017-12-21
wuqiong 2017-04-10
Lzn0 2017-03-27
fengyeezju 2015-12-25
superxlcr 2015-05-27
rookieding 2015-04-22
familynt 2015-04-06
mishaki 2015-02-13
江南以北 2014-12-28