我用下面的代码以编程方式创建视图采取并建立了PercentRelativeLayout.LayoutParams
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_percent_relative);
ViewGroup root = (ViewGroup)findViewById(R.id.perecent_layout_view);
View blueWidget = new View(this);
blueWidget.setBackground(blue);
root.addView(blueWidget);
setUpLayout(blueWidget, 2.0f/8.0f, 6.0f/8.0f, 6.0f/8.0f, 0.0f, 2.0f/8.0f, 0.0f);
View redWidget = new View(this);
redWidget.setBackground(red);
root.addView(redWidget);
setUpLayout(redWidget, 5.0f/8.0f, 2.0f/8.0f, 3.0f/8.0f, 0.0f, 0.0f, 6.0f/8.0f);
}
private void setUpLayout(View view, float widthPercent,
float heightPercent,
float leftMarginPercent,
float rightMarginPercent,
float topMarginPercent,
float bottomMarginPercent) {
PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(view.getLayoutParams());
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams.getPercentLayoutInfo();
percentLayoutInfo.heightPercent = heightPercent;
percentLayoutInfo.widthPercent = widthPercent;
percentLayoutInfo.topMarginPercent= topMarginPercent;
percentLayoutInfo.bottomMarginPercent= bottomMarginPercent;
percentLayoutInfo.leftMarginPercent= leftMarginPercent;
percentLayoutInfo.rightMarginPercent= rightMarginPercent;
view.setLayoutParams(layoutParams);
}
的看法是这样的:
你看过PercentRelativeLayout吗? – ditkin
它看起来很有前途!我会尝试配置一些内容并再次发表评论! **编辑:**我试图在Java中配置PercentRelativeLayout孩子的百分比,但它似乎(看我发现的例子:[example](http://stackoverflow.com/questions/32497833/how-to-set-如果使用XML定义子View,百分比只能通过Java解决。我需要动态地创建和定位对象,就像启动器窗口小部件一样。 谢谢! –