How to show Text on Image

后端 未结 1 1564
既然无缘
既然无缘 2021-01-05 08:39

I have written a code, in which i am showing images in GridView, but now i want to show text in bottom of every image.

And i want to show my Grid like this:

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-05 09:08

    You need to define a custom grid item layout which contains a textview to assign the text.

    
    
        
        
    
    

    Here we have created a FrameLayout which has the imageview set to match the parent's dimensions, this will be the imageview that displays the photo. Next, there is a TextView which will be used to display the item title and that is aligned to the bottom of the FrameLayout.

    Next, we need to edit your adapter to use this grid item layout and render the correct information.

    public View getView(int position, View convertView, ViewGroup parent) {
    
        // Inflate the single grid item layout
        if (convertView == null) {  
            convertView = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
        }
    
        // Set Image
        ImageView thumbnail = convertView.findViewById(R.id.thumbnail);
        if (thumbnail != null) {
            thumbnail.setImageResource(mThumbIds[position]);
        }
    
        // Set Text
        TextView title = convertView.findViewById(R.id.title);
        if (title != null) {
            title.setText("Image Number: " + position);
        }
    
        return convertView;     
    }
    

    mLayoutInflater should be globally defined in your constructor using

    mLayoutInflater = LayoutInflater.from(context);
    

    0 讨论(0)
提交回复
热议问题