Android Inflate layout to use inside TableRow

前端 未结 2 516
暗喜
暗喜 2021-01-25 14:00

I\'m trying to build a table in android dynamically. But when I attempt to inflate a view, for instance, table_cell.xml, and add it to my table row, nothing appears:

<         


        
相关标签:
2条回答
  • 2021-01-25 14:38

    Try This You have to provide id for positioning.

    TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
    
    View tableCell = getActivity().getLayoutInflater()
                    .inflate(R.layout.table_cell, container, false);
    
    TableRow tableRow = new TableRow(getActivity());
    
    tableRow.addView(tableCell);
    tableLayout.addView(tableRow,id);
    

    or

    TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
    
    View tableCell = getActivity().getLayoutInflater()
                    .inflate(R.layout.table_cell, container, false);
    
    TableRow tableRow = new TableRow(getActivity());
    
    TableRow.LayoutParams LP = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    
    tableRow.setLayoutParams(LP);
    tablerow.setId(ID);
    
    
    tableRow.addView(tableCell);
    tableLayout.addView(tableRow);
    

    or

    TableLayout tableLayout = (TableLayout)rootView.findViewById(R.id.tableLayout);
    
    View tableCell = getActivity().getLayoutInflater()
                    .inflate(R.layout.table_cell, container, false);
    
    TableRow tableRow = new TableRow(getActivity());
    
    
    tableRow.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT)); 
    tablerow.setId(ID);
    
    
    tableRow.addView(tableCell);
    tableLayout.addView(tableRow, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 2021-01-25 14:47

    You should try with getActivity().getLayoutInflater() .inflate(R.layout.table_cell, tableRow, true);

    And you should not add the tableCell view on the tableRow manually in this case as the inflator method takes care of that since we pass true ..

    0 讨论(0)
提交回复
热议问题