girdview 图标与文字同时存在

未分类 , , , , , 发表评论

1, 先定义GridView方格中的元素布局

res/layout/appsviewcell.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
  xmlns:android=”http://schemas.android.com/apk/res/android”
  android:id=”@+id/appsviewcell_layout”
  android:orientation=”vertical”
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent”>
    
    <ImageView 
    android:id=”@+id/appsviewcell_image”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_centerHorizontal=”true”/>
    
    <TextView 
    android:id=”@+id/appsviewcell_text”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_centerHorizontal=”true”
    android:gravity=”center”
    android:layout_below=”@+id/appsviewcell_image”/>
</RelativeLayout>

里面用到的一些参数在res/values/strings.xml中定义

关键部分:

@Override
public View getView(int position, View cellView, ViewGroup vgroup) {
// TODO Auto-generated method stub

AppsViewItemViewInfo itemViewInfo = null;
if(null == cellView)
{
cellView = this.mInflater.inflate(R.layout.appsviewcell, null);

ImageView iv = (ImageView)cellView.findViewById(R.id.appsviewcell_image);
TextView tv = (TextView)cellView.findViewById(R.id.appsviewcell_text);

AppsViewItemInfo appItemViewInfo = this.aviis[position];

iv.setImageResource(appItemViewInfo.getAppIcon());
String appname = appItemViewInfo.getAppName();
Log.d(“DEBUG”, “Position = “+position+”, AppName = “+appname);
tv.setText(appname);

itemViewInfo = new AppsViewItemViewInfo();
itemViewInfo.setViewImage(iv);
itemViewInfo.setViewText(tv);

cellView.setTag(itemViewInfo);
}
else
{
itemViewInfo = (AppsViewItemViewInfo)cellView.getTag();
}

return cellView;
}

上一篇:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *