我想在每张图片下面添加一个文字,这是我的实际代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
imageView.setLayoutParams(new GridView.LayoutParams(
grid.getWidth()/2, grid.getHeight()/3));
return imageView;
}
答案 0 :(得分:1)
试试这个,创建像这样的布局
<?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="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/child_Img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/photos_grid_bg"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/title_Txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text=""
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
像这样给你的布局充气
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder mHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.photos_grid_item, null);
mHolder = new ViewHolder();
mHolder.Img = (ImageView) convertView
.findViewById(R.id.Img);
mHolder.title_Txt = (TextView) convertView
.findViewById(R.id.title_Txt);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
try {
mHolder.Img.setImageResource(mThumbIds[position]);
mHolder.title_Txt.setText("Set your text here");
} catch (Exception e) {
// TODO: handle exception
}
return convertView;
}
private class ViewHolder {
private ImageView Img;
private TextView title_Txt;
}
答案 1 :(得分:1)
LinearLayout layout = new LinearLayout(mContext);
layout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
mageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
imageView.setLayoutParams(new LinearLayout.LayoutParams(
grid.getWidth()/2, grid.getHeight()/3));
layout.addView(imageView);
TextView tv = new TextView(mContext);
tv.setLayoutParams(new LinearLayout.LayoutParams(
grid.getWidth()/2, grid.getHeight()/3));
tv.setText("hello");
layout.addView(tv);
return layout;
试试这个,您可以根据您的设计稍后调整视图大小