java 代码 添加控件 修改位置 View

Button button = new Button(this);
FrameLayout frameLayout = getWindow().getDecorView().findViewById(android.R.id.content);
frameLayout.addView(button);

//FrameLayout.LayoutParams layout= (FrameLayout.LayoutParams) button.getLayoutParams();
//layout.setMargins(100,120,0,0);
//layout.width = 200;
//layout.height = 200;
//button.setLayoutParams(layout);

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.TOP | Gravity.START;
params.setMargins(13, 500, params.rightMargin, params.bottomMargin);
button.setLayoutParams(params);

添加一个Button,修改其高度、宽度、以及位置。

 

RelativeLayout设置齐对齐方式

View view = LayoutInflater.from(StationActivity.this).inflate(R.layout.layout_station_like_toast, null);
layout_main.addView(view);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(params.leftMargin, params.topMargin, 0, 0);
view.setLayoutParams(params);

 

posted on 2019-08-08 14:23  赵子隆  阅读(797)  评论(0编辑  收藏  举报

导航