我在运行时在FrameLayout中添加按钮,我想在父级的右下角设置。
LayoutParams lp = new LayoutParams(android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = **Gravity.RIGHT**;
mSelf.setLayoutParams(lp);
我该怎么做,有什么建议吗?
答案 0 :(得分:1)
在framelayout中添加相对布局,并在该relativelayout中添加组件
使用示例编辑
FrameLayout fr = ..... // get your layout here.
RelativeLayout mRel = new RelativeLayout(your_activity_context);
RelativeLayout.LayoutParams mParams = mRel.getLayoutParams();
Button mBtn = new Button(this); // Component you want to add at BOTTOM RIGHT
mParams.addRule(RelativeLayout.LayoutParams.ALIGN_PARENT_BOTTOM | RelativeLayout.LayoutParams.ALIGN_PARENT_RIGHT);
mBtn.setLayoutParams(mParams);
mRel.add(mBtn);
fr.add(mRel);
注意: - 如果您在活动中添加此代码,代码中会有一些细微的变化。因为我这里只写了这个。没有测试过。但这应该有用。
答案 1 :(得分:0)
试试这个代码。它可以帮到你。
RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);