Android RelativeLayout Définir par programmation “centerInParent”
J'ai un RelativeLayout comme ceci:
<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>
Je veux être en mesure de définir par programmation pour positiveButton
le même effet que:
android:layout_centerInParent="true"
Comment puis-je en faire par programmation ?
Vous devez vous connecter pour publier un commentaire.
Complètement non testé, mais ce devrait travail:
ajouter
android:configChanges="orientation|screenSize"
à l'intérieur de votre activité dans votre manifesteif (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
où0
effectivement évaluée àfalse
RelativeLayout
et pas avecLinearLayout
!Juste pour ajouter une autre saveur de Reuben réponse, je l'utilise comme ceci pour ajouter ou de supprimer cette règle conformément à une condition:
J'ai fait pour
1. centerInParent
2. centerHorizontal
3. centerVertical
avec vrai et faux.
Comment appeler la méthode:
centerInParent - vrai
centerInParent - faux
centerHorizontal - vrai
centerHorizontal - faux
centerVertical - vrai
centerVertical - faux
Espère que cela peut vous aider.