1

我想以百分比设置 PercentRelativeLayout 的高度。

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

        myLayout.addView(layout);

我想在 LayoutParams 中使用 percentHeight(50%),而不是 Match_Parent 或 Wrap_Content。

4

1 回答 1

2

这段代码:

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;

会有这个输出:

PercentRelativeLayout 占用屏幕高度的一半

于 2017-03-27T16:29:00.197 回答