如果想要在代码中设置 android:layout_centerInParent属性,则可以在代码中这样写: layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);