2012-10-09

android:layout_centerHorizontal in code

You can set android:layout_centerHorizontal in XML:

<Button
    ...
    android:layout_centerHorizontal="true"
    ...
/>
But how to set it dynamically in code?
Here is the answer:

LayoutParams layoutparams = new LayoutParams(width, height);
layoutparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mButton.setLayoutParams(layoutParams);

No comments: