创建了动态linearlayout。创建了两个button:
论文问答
1
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
TextView titleView = new TextView(this);
titleView.setWidth(LayoutParams.WRAP_CONTENT);
titleView.setHeight(LayoutParams.WRAP_CONTENT);
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
titleView.setText("Hallo Welt!");
layout.addView(titleView);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
layout.addView(btnConnect);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
layout.addView(btnDisconnect);
我想将链接的button放到左角,没链接的button放到右角,不知道能不能实现? 谢谢:D
-
试试设置button布局
LayoutParams params; Button btnConnect = new Button(this); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.Left; btnConnect.setLayoutParams(params); ... Button btnDisconnect = new Button(this); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.Right; btnConnect.setLayoutParams(params);
-
用xml布局实现比较简单 layout.addView(btnConnect); layout.addView(titleView); layout.addView(btnDisconnect);
这三句代码放到最后写即可实现 不过只是从相对的左右 不是相对界面而是相对各个控件 如果要相对界面的话那就要用Framelayout或者Relatilayout了
发表回复