I
I
Igor2014-05-10 12:14:49
Android
Igor, 2014-05-10 12:14:49

Why does TableLayout expand in height when adding a TableRow programmatically?

There is a TableLayout inside SrcollView. Height as you can see wrap_content.
I add TableRow-s dynamically and the TableLayout expands in height. That is, only two lines are added, and a lot of free space is occupied.
TableRow is colored orange, TableLayout is red.
How to make TableLayout height equal to sum of TableRow heights?

<ScrollView
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:fillViewport="true"
>
 <LinearLayout
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:orientation="vertical"
  >
   <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:id="@+id/ordersTableLayout"
    android:background="@color/red"
   >

   </TableLayout>
  </LinearLayout>
</ScrollView>

final TableLayout tableLayout = (TableLayout) getView().findViewById(R.id.ordersTableLayout);
    tableLayout.removeAllViews();

    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < basketData.getGoodItems().size(); i++) {
      final View view = inflater.inflate(R.layout.order_item2, null);
      TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
      view.setLayoutParams(layoutParams);

      BasketData.GoodItem goodItem = basketData.getGoodItems().get(i);

      float price = Float.parseFloat(goodItem.getItem().getPrice());

      TextView orderNameTextView = (TextView) view.findViewById(R.id.orderNameTextView);
      TextView orderCountTextView = (TextView) view.findViewById(R.id.orderCountTextView);
      TextView orderPriceTextView = (TextView) view.findViewById(R.id.orderPriceTextView);

      orderNameTextView.setText( Html.fromHtml(goodItem.getItem().getName()) );
      orderPriceTextView.setText(goodItem.getCount() * price + "p");
      orderCountTextView.setText(goodItem.getCount() + "");

      tableLayout.addView(view, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
    }

03711313f2da4272be1be24a0e677737.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
StanKo, 2014-05-12
@StanKo

In my opinion, everything is as it should be, the content wraps normally in the scroll, because it occupies the entire screen, otherwise you need to limit the height of the TableView to 1 line, or ScrollView. Or use ListView.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question