alllllllll
2013-03-05 09:39
采纳率: 84.2%
浏览 6.0k

将一个button放左边另一个放右边

创建了动态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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • love_loveec 2013-03-06 02:55
    已采纳

    试试设置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);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题