Android:如何动态自定义GridView布局?

时间:2012-11-26 15:47:15

标签: android android-layout android-gridview

我创建了一个生成GridView的应用程序。每个网格包含框。生成的框数取决于从数据库中检索的名称的数量。

这是我的代码: main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView 
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth" 
        android:orientation="horizontal"
        android:layout_marginTop="100dp"
 />

layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_gravity="center"
>

<ImageView
    android:id="@+id/flag"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:src="@drawable/chair"
/>

</LinearLayout>

main.class的一部分

ArrayList<HashMap<String, String>> take = new ArrayList<HashMap<String, String>>();

try 
{
      JSONObject jArray = new JSONObject(result);

            JSONArray stud_info = jArray.getJSONArray("info");
            ca_list = new ArrayList<ViewGridObject>();

            for (int i = 0; i < info.length(); i++) 
            {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = stud_info.getJSONObject(i);

                map.put("id", String.valueOf(i));
                map.put("flag", Integer.toString(flags[0]));
                take.add(map);

                ca_list.add(new ViewGridObject(R.drawable.chair));
            }

            myadapter =  new AdapterViewGrid(main.this, list);
            gridView.setAdapter(myadapter);


        } catch (JSONException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

目前,它会显示这样的GridView。Current GridView generated

但我想动态定制gridview的布局。我希望它像这样..

Desired GridView

请。帮助..

0 个答案:

没有答案