动态表格布局样式

时间:2017-01-27 10:45:39

标签: java android dynamic grid tablerow

我正在开发一个Android项目,用户可以根据条形码查询他/她的库存。一切都按预期工作,只有" SizeTable Grid"没有正确显示。

enter image description here

但我希望如此:

enter image description here

我为每个'对象'声明了一些LayoutParams。我添加到父母:

LinearLayout.LayoutParams sp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

这一个用于tableRows中的textViews(相同但有2.0f):

TableRow.LayoutParams lpHeaderTxt = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 2.0f);

结果将是:

 - LinearLayout (Created on the design as main parent object)
  - Textfield (To hold the store-name)
  - Horizontal scroll (To hold the grid table)
    - Table
      - Multiple rows 
        - Multiple headers

我的表是使用3个循环动态生成的。

- Loop all the stores
 | Create header row with descriptions |
 - Loop all the Y-Sizes (Rows)
  | Create first col with Y-description |
  - Loop all the X-Sizes (Cols) 
   | Create col with the value, matching the X-Y size |

我的代码(缩短):

    // Define new LinearLayout
    // - Wil contain everything (every store and every grid)
    LinearLayout linearStock = (LinearLayout) view.findViewById(R.id.linearStock);
    LinearLayout.LayoutParams sp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    linearStock.setLayoutParams(sp);

    // ... Code .. //


        // Start looping

        // Define Params for easier reference to know which params to use (redundant I know..)
        TableRow.LayoutParams lpRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
        TableRow.LayoutParams lpTable = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
        TableRow.LayoutParams lpStoreField = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
        TableRow.LayoutParams lpHeaderCol = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 2.0f);
        TableRow.LayoutParams lpHeaderTxt = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 2.0f);

        // Loop stores
        for(int i = 0; i < arrayStores.length(); i++){
            // Get the key (ex. 02)
            String StoreKey = arrayStores.getString(i);

            // Create Header row
            TableRow rowHeader = new TableRow(getActivity());

            // Set layout params
            rowHeader.setLayoutParams(lpRow);

            if(isMultiDimensional){
                // Add spacer
                // |  |
                TextView txtSizeHeader = new TextView(getActivity());

                txtSizeHeader.setLayoutParams(lpHeaderTxt);
                txtSizeHeader.setText(" ");
                txtSizeHeader.setTextSize(16);
                txtSizeHeader.setTypeface(null, Typeface.BOLD);
                rowHeader.addView(txtSizeHeader);
            }

            // Loop sizeX-descriptions
            // After this loop: |  | S | M | L | ..
            for(int iSizeTableX = 0; iSizeTableX < arrayXObj.length(); iSizeTableX++){
                // Fetch key value (ex. S)
                String headerXkey = arrayXObj.getString(iSizeTableX);

                TextView txtSizeHeader = new TextView(getActivity());
                txtSizeHeader.setLayoutParams(lpHeaderTxt);
                txtSizeHeader.setText(sizeXObj.getString(headerXkey));
                txtSizeHeader.setTextSize(16);
                txtSizeHeader.setTypeface(null, Typeface.BOLD);
                rowHeader.addView(txtSizeHeader);
            }

            // Create Store Field
            // (Ex. Webshop)
            TextView txtStore = new TextView(getActivity());
            txtStore.setLayoutParams(lpStoreField);
            txtStore.setText(inheritStore.get(StoreKey).toString());
            linearStock.addView(txtStore);

            // Create new Table for the stock grid
            TableLayout tableStock = new TableLayout(getActivity());
            tableStock.setLayoutParams(lpTable);
            tableStock.setStretchAllColumns(true);

            // Start Creating the grid
            // 1. Add Header row
            // |  | S | M | L | ..
            tableStock.addView(rowHeader);

            // If multi-d sizetable, loop y-sizes first
            if(isMultiDimensional) {

                for(int iSizeTableY = 0; iSizeTableY < arrayYObj.length(); iSizeTableY++){

                    String SizeYkey = arrayYObj.getString(iSizeTableY);
                    // (Ex. L 32)

                    // Start new row (for y-size with all x-sizes)
                    TableRow rowNewYSize = new TableRow(getActivity());
                    rowNewYSize.setLayoutParams(lpRow);
                    // Create new textfield for the value

                    TextView txtSizeYDescription = new TextView(getActivity());
                    txtSizeYDescription.setLayoutParams(lpHeaderTxt);
                    txtSizeYDescription.setText(sizeYObj.get(SizeYkey).toString());
                    rowNewYSize.addView(txtSizeYDescription);

                    // Start looping X-Sizes
                    for(int iSizeTableX = 0; iSizeTableX < arrayXObj.length(); iSizeTableX++){

                        String SizeXkey = arrayXObj.getString(iSizeTableX);
                        // (Ex. 40)

                        TextView txtStockValue = new TextView(getActivity());
                        txtStockValue.setLayoutParams(lpHeaderTxt);

                        // build keyCode
                        // (Ex. 010402)
                        String keyCode = StoreKey + leftPad(SizeXkey, 2, "0") + leftPad(SizeYkey, 2, "0");
                        // Get Stock value
                        Integer stockVal = 0;
                        // Try to get the stock value based on matching key
                        try {
                            stockVal = jsonStockGrid.getInt(keyCode);
                        }catch (Exception e){
                        }

                        txtStockValue.setText(stockVal.toString());

                        // Add value to the row
                        rowNewYSize.addView(txtStockValue);
                    }
                    // Add y-row to the table
                    tableStock.addView(rowNewYSize);
                }

                // If not multi-d
            }else{
                // .. Same stuff except not looping Y-Sizes .. //
            }

            HorizontalScrollView scroll = new HorizontalScrollView(getActivity());
            scroll.setLayoutParams(lpRow);
            // Add the table to the scroll object
            scroll.addView(tableStock);

            // Add the scroll to the layout object
            linearStock.addView(scroll);

      // ... Code .. //

这种方式可能很脏,但它有效!

我错过了哪个参数&#34;伸展&#34;列到全宽或至少是一个在水平滚动中滚动的更大的立方体?

我还需要找到一些方法来根据键值对JSON对象进行排序:

{  02 : M, 03 : L, 01 : S }

但那是另一次了!

**关于排序的更新**

对任何有兴趣的人。这就是我排序&#39;我的JSONObject 示例:SizeX(S,M,L)

    // Create child object from parent
    sizeXObj = sizesObj.getJSONObject("sizeX");
    // Convert to JSONArray
    arrayXObj = sizeXObj.names();  //<<< get all keys in JSONArray
    // Loop JSONArray Length
    for(int iSizeTableX = 0; iSizeTableX < sizeXObj.length(); iSizeTableX++) {
        // Fetch key value (ex. S)
        String headerXkey = arrayXObj.getString(iSizeTableX);
        // Add to List
        listSizeX.add(headerXkey);
    }
    // Sort list
    Collections.sort(listSizeX);
    // Iterate list and get JSONObject value by the iterator key..

**回答了我自己的问题**

短暂休息之后,很清楚为什么桌子不是全尺寸的。 textWidth只有1个字符。如果布局是,对于每个孩子,匹配..容器将只与所有字符的concat一样宽。

解决方案:

// Define width of cell..
Integer tableColumnWidth = 100;
// Add this to each textView in every table cell
txtSizeHeader.setWidth(tableColumnWidth);

enter image description here

0 个答案:

没有答案