創建了動態linearlayout。創建了兩個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放到右角,不知道能不能實現?
謝謝: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);