以图片和文字的Andr​​oid GridView控件 [英] gridview with image and text android

查看:122
本文介绍了以图片和文字的Andr​​oid GridView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地创建填充了图像的GridView。现在,我想要做的就是把图像下方的文本。

I successfully created a gridview populated with images. Now, what I want to do is put a text below the image.

这是我的应用程序的截图为止。

This is the screenshot of my app so far.

http://i203.photobucket.com/albums/aa266/ paulocarabuena_bucket /屏幕2.png

下面是我getView方法我imageadapter类..

Here is my getView method in my imageadapter class..

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

if (convertView == null) {
    imageView = new ImageView(this.context);
    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(this.thumbs[position]);
return imageView;
}

下面是我的主要活动的onCreate方法

Here is the onCreate method of my main activity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.launchFullScreen();
    this.setContentView(R.layout.main);

    this.grids = (GridView) this.findViewById(R.id.elements);

    ImageAdapter adapter = new ImageAdapter(this);

    this.grids.setAdapter(adapter);

    this.grids.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

        }
    });
}

在此先感谢。 :)

Thanks in advance. :)

推荐答案

您可以使用XML,定义 GridView控件细胞做到这一点:

You can do it using an XML, that defines the GridView cell:

image.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
    >
    <ImageView
        android:id="@+id/image"
        android:layout_marginBottom = "10dp"
        android:src="@drawable/cell_sfondo"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ImageView>

<TextView
        android:id="@+id/title" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

然后在你的 getView()

if(convertView == null) {
            view = inflater.inflate(R.layout.image, null);
        }
        else
            view = convertView;
ImageView image = (ImageView)view.findViewById(R.id.image);
image.setImageResource(this.thumbs[position]);

这篇关于以图片和文字的Andr​​oid GridView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆