tengo un Disposición relativa como esto:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:background="@drawable/black_menu_button"
android:layout_marginLeft="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:background="@drawable/blue_menu_button"
android:layout_marginRight="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Quiero poder configurar programáticamente para positiveButton
el mismo efecto que:
android:layout_centerInParent="true"
¿Cómo puedo hacer esto programáticamente?
Completamente sin probar, pero esto debería trabajar:
View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);
agregar android:configChanges="orientation|screenSize"
dentro de su actividad en su manifiesto
Solo para agregar otro sabor de la respuesta de Reuben, lo uso así para agregar o eliminar esta regla de acuerdo con una condición:
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) holder.txtGuestName.getLayoutParams();
if (SOMETHING_THAT_WOULD_LIKE_YOU_TO_CHECK) {
// if true center text:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
holder.txtGuestName.setLayoutParams(layoutParams);
} else {
// if false remove center:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
holder.txtGuestName.setLayoutParams(layoutParams);
}
he hecho por
1. centroEnPadre
2. centroHorizontal
3. centroVertical
con verdadero y falso.
private void addOrRemoveProperty(View view, int property, boolean flag){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
if(flag){
layoutParams.addRule(property);
}else {
layoutParams.removeRule(property);
}
view.setLayoutParams(layoutParams);
}
Cómo llamar al método:
centerInParent – verdadero
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
centerInParent – falso
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
centroHorizontal – verdadero
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
centroHorizontal – falso
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
centroVertical – verdadero
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
centroVertical – falso
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
Espero que esto te ayude.