android 动态添加控件位置,android – 如何在布局中的动态位置添加按钮?

使用RelativeLayout将控件放在您喜欢的位置.看看这个链接:

Dynamic TextView in Relative layout

和这里

How to create a RelativeLayout programmatically with two buttons one on top of the other?

如果您只想在XML中实现它.看这里:

http://www.mkyong.com/android/android-relativelayout-example/

这里有一个示例如何使用RelativeLayout动态定位控件:

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Creating a new RelativeLayout

RelativeLayout mainRelativeLayout = new RelativeLayout(this);

// Defining the RelativeLayout layout parameters with Fill_Parent

RelativeLayout.LayoutParams relativeLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

// Creating a new Left Button

Button buttonLeft = new Button(this);

buttonLeft.setText("Left");

// Creating a new Left Button with Margin

Button buttonLeftWithMargin = new Button(this);

buttonLeftWithMargin.setText("Left with Margin");

// Creating a new Center Button

Button buttonCenterParent = new Button(this);

buttonCenterParent.setText("Center");

// Creating a new Bottom Button

Button buttonBottom = new Button(this);

buttonBottom.setText("Bottom");

// Add a Layout to the Buttons

AddButtonLayout(buttonLeft, RelativeLayout.ALIGN_PARENT_LEFT);

AddButtonLayout(buttonCenterParent, RelativeLayout.CENTER_IN_PARENT);

AddButtonLayout(buttonBottom, RelativeLayout.ALIGN_PARENT_BOTTOM);

// Add a Layout to the Button with Margin

AddButtonLayout(buttonLeftWithMargin, RelativeLayout.ALIGN_PARENT_LEFT, 30, 80, 0, 0);

// Add the Buttons to the View

mainRelativeLayout.addView(buttonLeft);

mainRelativeLayout.addView(buttonCenterParent);

mainRelativeLayout.addView(buttonBottom);

mainRelativeLayout.addView(buttonLeftWithMargin);

// Setting the RelativeLayout as our content view

setContentView(mainRelativeLayout, relativeLayoutParameters);

}

private void AddButtonLayout(Button button, int centerInParent, int marginLeft, int marginTop, int marginRight, int marginBottom) {

// Defining the layout parameters of the Button

RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

// Add Margin to the LayoutParameters

buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);

// Add Rule to Layout

buttonLayoutParameters.addRule(centerInParent);

// Setting the parameters on the Button

button.setLayoutParams(buttonLayoutParameters);

}

private void AddButtonLayout(Button button, int centerInParent) {

// Just call the other AddButtonLayout Method with Margin 0

AddButtonLayout(button, centerInParent, 0 ,0 ,0 ,0);

}

}

你应该得到这样的东西:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值