CardView has lost margin when inflating

后端 未结 1 554
鱼传尺愫
鱼传尺愫 2020-12-20 12:15

In my activity, I am setting the layout activity_main onCreate. I am then wanting to inflate my CardView for each of the items in my array.

So far, I\'ve got every

相关标签:
1条回答
  • 2020-12-20 12:51

    You're passing in null as the parent ViewGroup parameter to inflate(). This will cause all layout_* attributes to be ignored, as the inflater has no idea which attributes are valid for the container in which it will be placed (i.e. it has no idea which LayoutParams type to set on the View).

    View child = getLayoutInflater().inflate(R.layout.activity_main_card, null);
    

    should be

    View child = getLayoutInflater().inflate(R.layout.activity_main_card, item, false);
    

    For more info, see this great article about it -- it's a common mistake.

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