gpt4 book ai didi

android - getView 方法在图像下方添加文本

转载 作者:行者123 更新时间:2023-11-29 21:08:45 24 4
gpt4 key购买 nike

我想在每张图片下方添加一个文本,这是我的实际代码:

@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;
}

最佳答案

试试这个,创建这样的布局

<?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;
}

关于android - getView 方法在图像下方添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com