WRAP_CONTENT not working after dynamically adding views

后端 未结 8 931
借酒劲吻你
借酒劲吻你 2020-12-05 02:28

I\'m trying to create a fragment that lays out a series of custom views dynamically. The main content for this layout is a RelativeLayout nested in a LinearLayout (to center

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 02:36

    Ok, I solved this by manually measuring the RelativeLayout immediately after adding all the views and setting the mainLayoutParams height explicitly. I wish I was smarter and knew why it wasn't automatically doing this correctly in the first place, but oh well.

        ...
        mainLayout.measure(0, 0);
    
        ViewGroup.LayoutParams mainLayoutParams = mainLayout.getLayoutParams();
        mainLayoutParams.height = mainLayout.getMeasuredHeight() + 10;
        ...
    

提交回复
热议问题