在java中设置属性

时间:2011-05-17 22:40:12

标签: android attributes android-layout

在我的布局xml中,如果我执行类似android:layout_marginTop="10px"的操作,我怎样才能在java中执行相同的操作?

1 个答案:

答案 0 :(得分:0)

试试这个:

RelativeLayout rel=new RelativeLayout(this);
mWidth=getWindowManager().getDefaultDisplay().getWidth();
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(mWidth,mHeight);
Button mBtn=(Button) findViewById(R.id.xBtn);
params=new RelativeLayout.LayoutParams(mWidth, mHeight);
params.setMargins(int left, int top, int right, int bottom);
mBtn.setLayoutParams(params);