1

Please have a look at the following code:

        LinearLayout ll1 = new LinearLayout(context);
        ll1.setBackgroundColor(Color.BLUE);
        ll1.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        ll1LayoutParams.setMargins(100, 0, 100, 0);
        ll1.setLayoutParams(ll1LayoutParams);
        ...
        // parentLayout is FrameLayout
        parentLayout.addView(ll1, ll1LayoutParams);

Why doesn't it work?

toom
  • 12,864
  • 27
  • 89
  • 128

1 Answers1

2

Change

LinearLayout.LayoutParams ll1LayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

to

FrameLayout.LayoutParams ll1LayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

When assigning layout params to a child, you must assign the LayoutParams class of its parent and not the view. Since here your parent view is a FrameLayout, you have to use FrameLayout.LayoutParams.

Raghu Teja
  • 235
  • 1
  • 11