TableLayout (TableRow?) not sizing child Views as expected when adding them dynamically

后端 未结 3 563
-上瘾入骨i
-上瘾入骨i 2021-01-24 15:30

Context: I have a TableLayout (created using XML), which has one TableRow, which has one TextView. The code:

 

        
3条回答
  •  清酒与你
    2021-01-24 16:21

    The problem is this behavior defined for TableRow:

    The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

    Rather than add your text views directly to the TableRow, have the TableRow hold a horizontal LinearLayout and add the second view to that holder.

    (Also, using LinearLayout.LayoutParams for something that's going into a TableRow is wrong. You should have been using TableRow.LayoutParams. But that wouldn't be the way to get equal-width TextViews. Use a LinearLayout holder.)

提交回复
热议问题