动态网格布局 [英] Dynamic Grid Layout

查看:419
本文介绍了动态网格布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现网格,这将在动态填充。我想知道是什么来实现这个相对布局(列表视图)或最好的办法网格布局?

I want to implement grid, which will be populated dynamically. I want to know what is the best approach to implement this Relative layout(List View) or Grid Layout?

推荐答案

您可以动态地生成一个GridView。

You can generate a GridView dynamically.

GridView控件将包含的ImageView和TextView中的按照您的需要。你将不得不使用自定义的适配器。在它的 getView 法,填充的ImageView和TextView的。

GridView would contain of ImageView and TextView as per your need. You will have to use your custom adapter. In it's getView method, populate the ImageView and TextView.

示例:

GridView控件item.xml:

GridView item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/txtItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="trebuchet"
        android:textColor="@android:color/black"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

Java的code:

Java code:

一个POJO类项目:

public class Item
{
    String title;
    Drawable image;
    //getter setter
}

适配器类:

//getView method in your adapter class

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View itemView = convertView;
    ViewHolder holder = null;

    if (itemView == null)
    {
        final LayoutInflater layoutInflater =
            (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = layoutInflater.inflate(resourceId, parent, false);

        holder = new ViewHolder();
        holder.imgItem = (ImageView) itemView.findViewById(R.id.imgItem);
        holder.txtItem = (TextView) itemView.findViewById(R.id.txtItem);
        itemView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) itemView.getTag();
    }

    Item item = getItem(position);
    holder.imgItem.setImageDrawable(item.getImage());
    holder.txtItem.setText(item.getTitle());

    return itemView;
}

现在在你的Activity类中添加适配器的数据,然后设置适配器的GridView。

Now add adapter data in your Activity class and then set that adapter to GridView.

请参阅本和的这个

希望它帮助。

这篇关于动态网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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