Relative Layout ignoring setMargin()

前端 未结 1 463
轻奢々
轻奢々 2021-02-19 00:44

I\'m trying to nest a RelativeLayout inside of a LinearLayout, and give the RelativeLayout a margin that spaces it\'s edges away from the

相关标签:
1条回答
  • 2021-02-19 01:02

    Edit:

    You are using MarginLayoutParams instead of LinearLayoutParams. NOTE: if you set LayoutParams on a layout container, you will need to use the parent's type.

    So if you have a LinearLayout that contains a RelativeLayout, you need to set LinearLayout.LayoutParams on the RelativeLayout.

    // NOT WORKING: LinearLayout.LayoutParams params = (LayoutParams) getLayoutParams();
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    int tbm = AllConversationsActivity.topAndBottomMargin;
    int lrm = AllConversationsActivity.leftAndRightMargin;
    params.setMargins(tbm, lrm, tbm, lrm);
    setLayoutParams(params);
    
    0 讨论(0)
提交回复
热议问题