如何在imageview中添加文本视图

时间:2016-09-21 12:21:04

标签: android android-studio

我是android新手。我用图像视图创建了一个网格视图。我想在图像视图中添加两个文本视图叠加层。

Hellogridview.java

public class HelloGridView extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });
}
}

ImageAdapter.java

    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(350, 350));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(10, 10, 10, 10);
    } else {
        imageView = (ImageView) convertView;
    }

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

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

我想在图像视图中显示两个文本视图。文本将从数组中获取。任何帮助将不胜感激。

0 个答案:

没有答案