【问题标题】:GridView with different number of columns according to the number of row根据行数不同列数的GridView
【发布时间】:2014-03-03 09:58:32
【问题描述】:

我正在尝试 2 天,我正在尝试为视图制作 UI,在第一行有两列,然后在第二行有三列,第三行再有两列,依此类推..而且我有实现动态数据(意味着行数/列数是动态的)。

我没有任何想法来实现这种类型的视图。我也尝试过分散的网格视图,但这是用于动态视图..但是在这个我有静态 2,3,2,3... 列1,2,3,4..rows。请帮帮我。任何教程都会很有帮助。

【问题讨论】:

  • 你可以使用表格布局
  • 感谢您的回复,但我有动态数据,数组的长度约为50,那么在表格布局中我必须在运行时添加每一行和每一列,我认为这会更复杂.

标签: android android-gridview


【解决方案1】:

我终于解决了这个问题。我尝试了 Raghunandan 和 user3278416 的建议。除了表格布局之外,我在运行时做了所有的事情。这是 xml 文件:

 <ScrollView  android:layout_width="match_parent"
    android:layout_height="match_parent">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/table_layout"
     >

这两种布局一个是小布局(第二行),一个是大布局(第一行):

  <?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" >
<RelativeLayout
android:layout_width="130dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="@drawable/big_product_bg" >
<ImageView
    android:id="@+id/img_offersrowlayout"
    android:layout_width="120dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:scaleType="fitXY"
    android:src="@drawable/img_bydefault" />

<TextView
    android:id="@+id/txt_title_offerrowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_offersrowlayout"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:singleLine="true"
    android:text="BenQ Digital camera"
    android:textColor="@color/green_app"
    android:textSize="15dp"
    android:textStyle="bold" />

小的:

<RelativeLayout
android:layout_width="90dp"
android:layout_height="160dp"
android:layout_margin="3dp"
android:background="@drawable/big_product_bg" >

<ImageView
    android:id="@+id/img_offersrowlayout"
    android:layout_width="90dp"
    android:layout_height="80dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:scaleType="fitXY"
    android:src="@drawable/img_bydefault" />

<TextView
    android:id="@+id/txt_title_homesmallrowlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/img_offersrowlayout"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:singleLine="true"
    android:text="BenQ Digital camera"
    android:textColor="@color/green_app"
    android:textSize="15dp"
    android:textStyle="bold" />

现在是 Java 代码:

 int leftMargin_small=2;
int topMargin_small=5;
int rightMargin_small=2;
int bottomMargin_small=5;

   int j = 0;
   while (Show.size()>j) {

try {
     LayoutInflater inflater =       (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     TableRow.LayoutParams param = new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 1.0f);
     param.setMargins(leftMargin_small, topMargin_small, rightMargin_small, bottomMargin_small);
     LinearLayout layout1 =  new LinearLayout(getApplicationContext());
     TableRow.LayoutParams param_layout = new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT, 1.0f);
     layout1.setLayoutParams(param_layout);
View question = inflater.inflate(R.layout.offers_rowlayout, null);

question.setLayoutParams(param);
//question.setId(pos);
TextView title = (TextView) question.findViewById(R.id.txt_title_offerrowlayout);
title.setText(""+">>"+Show.get(j).get("name"));

View question1 = inflater.inflate(R.layout.offers_rowlayout, null);
//question.setId(pos);
question1.setLayoutParams(param);
TextView title1 = (TextView) question1.findViewById(R.id.txt_title_offerrowlayout);
title1.setText(""+">>"+Show.get(j+1).get("name"));



View question_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question_small.setLayoutParams(param);
TextView title_small = (TextView) question_small.findViewById(R.id.txt_title_homesmallrowlayout);
title_small.setText(""+">>"+Show.get(j).get("name"));

View question1_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question1_small.setLayoutParams(param);
TextView title1_small = (TextView) question1_small.findViewById(R.id.txt_title_homesmallrowlayout);
title1_small.setText(""+">>"+Show.get(j+1).get("name"));

View question2_small = inflater.inflate(R.layout.home_row_small_layout, null);
//question.setId(pos);
question2_small.setLayoutParams(param);
TextView title2_small = (TextView) question2_small.findViewById(R.id.txt_title_homesmallrowlayout);
title2_small.setText(""+">>"+Show.get(j+2).get("name"));
       if (gett) {
      TableRow tr = new TableRow(getApplicationContext());

        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,1);
      //  trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);
       layout1.addView(question);
       layout1.addView(question1);
       tr.addView(layout1);
    tablee.addView(tr);
   gett = false;
        j = j+2;
}
else
{
      TableRow tr = new TableRow(getApplicationContext());

        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,1);

        layout1.addView(question_small);
        layout1.addView(question1_small);
        layout1.addView(question2_small);
        tr.addView(layout1);
    tablee.addView(tr);
    gett = true;
    j = j+3;
}

} catch (Exception e) {
    e.printStackTrace();
}


  }

这里Show是hashmap类型的arraylist,tablee是我在xml文件中制作的tablelayout对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多