1

I want to set the height of PercentRelativeLayout in percentage.

PercentRelativeLayout layout = new PercentRelativeLayout(this);
        PercentRelativeLayout.LayoutParams llp = new PercentRelativeLayout.LayoutParams(PercentRelativeLayout.LayoutParams.MATCH_PARENT, 50%);
        layout.setLayoutParams(llp);

        myLayout.addView(layout);

I suppose to use percentHeight(50%) in LayoutParams, not Match_Parent or Wrap_Content.

Onik
  • 19,396
  • 14
  • 68
  • 91
elbert rivas
  • 1,464
  • 1
  • 17
  • 15

1 Answers1

2

This code:

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root);
PercentRelativeLayout percentRelativeLayout = new PercentRelativeLayout(this);
percentRelativeLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
rootLayout.addView(percentRelativeLayout);

Button button = new Button(this);
percentRelativeLayout.addView(button);
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) button.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.5f;

Will have this output:

PercentRelativeLayout takes half the screen height

azizbekian
  • 60,783
  • 13
  • 169
  • 249