創(chuàng)建了動(dòng)態(tài)linearlayout。創(chuàng)建了兩個(gè)button: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放到右角,不知道能不能實(shí)現(xiàn)?謝謝:D
2 回答

阿波羅的戰(zhàn)車
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
試試設(shè)置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);

嗶嗶one
TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
用xml布局實(shí)現(xiàn)比較簡(jiǎn)單
layout.addView(btnConnect);
layout.addView(titleView);
layout.addView(btnDisconnect);
這三句代碼放到最后寫即可實(shí)現(xiàn) 不過(guò)只是從相對(duì)的左右 不是相對(duì)界面而是相對(duì)各個(gè)控件 如果要相對(duì)界面的話那就要用Framelayout或者Relatilayout了
添加回答
舉報(bào)
0/150
提交
取消