【问题标题】:Android - Set text inside imageview with gridviewAndroid - 使用 gridview 在 imageview 中设置文本
【发布时间】:2017-01-24 08:39:23
【问题描述】:

我想在图像视图中设置文本。目前我从drawable中获取图像,即使我认为使用viewholder更好?这是我的适配器:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,
            R.drawable.ic_round, R.drawable.ic_round,

    };
}

这是我想要使用的布局: item_single.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/single"
    >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_round" />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myImageView"
        android:layout_alignTop="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignBottom="@+id/myImageView"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text=""
        android:textColor="#000000" />

</RelativeLayout>

我看过这个问题How can I add a TextView into an ImageView in GridView Layout? 但没有明确的解释。在那个例子中,他使用了 ViewHolder,我的一直希望我使用 RecycleViewHolder。

【问题讨论】:

    标签: android gridview adapter android-viewholder


    【解决方案1】:

    你需要充气

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        TextView textView;
        View view; 
        if (convertView == null) {
            // if it's not recycled, initialize some attributes 
            view = LayoutInflater.from(mContext).inflate(R.layout. item_single,false);
            imageView = view.findViewById(R.id.myImageView);
            textView = view.findViewById(R.id.myImageViewText);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else { 
            view = convertView;
            imageView = convertView.findViewById(R.id.myImageView);
            textView = convertView.findViewById(R.id.myImageViewText);
        } 
    
        imageView.setImageResource(mThumbIds[position]);
        textView.setText("Any Text");
        return view;
    } 
    

    我建议您使用查看器,但根据您的要求,您应该这样做。

    【讨论】:

    • view = LayoutInflater.from(mContext).inflate(R.layout.item_single,false); 无法解析方法 inflate(int, boolean)
    • 好的,开始工作了。如果我想在每个 imageview 上将 textview 增加 1 怎么办?
    【解决方案2】:

    扩充您的自定义视图,其中有 ImageViewTextView 两者。在 Adapter 类的 getView 方法中,一些

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
         // This view will contain TextView and ImageView
         convertView = inflater.inflate(R.layout.item_row, parent, false);
    

    更多搜索自定义Adapter例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多