Aligner le texte à gauche, cochez la case à droite
j'essaie de créer une mise en page pour un ListView, avec une case à cocher à droite et du texte à gauche. La case à cocher doit être alignée à droite et le TextView aligné à gauche de la rangée, par exemple:
------------------------
text checkbox
------------------------
text checkbox
------------------------
text checkbox
------------------------
c'est Ce que j'ai jusqu'à présent:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="8dip"
android:paddingBottom="8dip"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
/>
<CheckBox
android:id="@+id/chekcbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
/>
</RelativeLayout>
cependant, ce qui rend réellement est la boîte de texte superposant la case à cocher, à la gauche de la rangée.
13 réponses
puisque vous utilisez déjà une distribution relative, utilisez ses attributs:
Supprimer android:gravity
des enfants et les remplacer par android:layout_alignParentLeft="true"
pour le TextView et android:layout_alignParentRight="true"
pour la case à cocher.
qui positionne les enfants par rapport aux frontières des parents. Vous pouvez également ajouter android:layout_centerVertical="true"
à chaque enfant pour centrer les deux verticales dans chaque élément de la liste.
pour d'autres options et attributs voir la documentation .
utilisez CheckedTextView
pour remplacer tout ce que vous avez indiqué ci-dessus.
et pour obtenir la case de la case à cocher à droite , dans les attributs de la case à cocher
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
pour la première fois vous devez mettre button
à @null
android:button="@null"
si vous voulez déplacer android case à cocher à droite utiliser:
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
sinon si vous aimez avoir l'image personnalisée pour la case à cocher Utiliser:
android:drawableRight="@drawable/selector_check_box"
et pour la pesanteur réglée à droite:
android:gravity="right|center_vertical"
pleine action pour l'utilisation de personnaliser la case:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
<item android:state_window_focused="false" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
<item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_on_pressed" />
<item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_off_pressed" />
<item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
<item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
<item android:state_window_focused="false" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
<item android:state_window_focused="false" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
<item android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
<item android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
</selector>
images:
Ma solution dans ce cas est d'utiliser:
<CheckBox
...
android:button="@null"
android:drawableRight="@drawable/my_check"
...
/>
où my_radio pourrait être par exemple votre sélecteur personnalisé! Exemple de sélecteur:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/check_checked" />
<item android:state_checked="false" android:drawable="@drawable/check_unchecked" />
</selector>
À la base de Oibaf il est ou MSaudi la solution
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
pour ajouter
android:paddingLeft="0dp"
pour éviter l'espace vide à la case à cocher du côté gauche d'un appareil. Le lien source:
comment afficher la case à cocher android sur le côté droit?
répondu par zeusboy.
utilisez ceci comme votre liste / article recycleur. Les 2 lignes ci-dessous sont la clé.
android: textDirection=" ltr" android: layoutDirection= " rtl "
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:textDirection="ltr"
android:layoutDirection="rtl"
/>
après avoir essayé de le faire moi-même, je l'ai finalement fait. Vous avez juste besoin de mettre un TextView et une case à cocher dans un layout horizontal, et mettre la gravité sur le layout à droite.
voici le code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/LabelText"
/>
<CheckBox
android:id="@+id/ChecKBox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
modifier: Je viens de remarquer que vous vouliez texte tout le chemin du côté gauche de l'écran. la case à cocher sera alors placée à droite, et le texte juste à côté, du côté gauche de la case à cocher.
comme ceci:
------------------------
text checkbox
------------------------
text checkbox
------------------------
text checkbox
------------------------
si vous allez utiliser un relativevelayout vous pourriez vouloir envisager d'utiliser les attributs relativevelayout moins ennuyeux, alignParentLeft et alignParentRight, comme suit:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="8dip"
android:paddingBottom="8dip"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentLeft="true"
/>
<CheckBox
android:id="@+id/chekcbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentRight="true"
/>
</RelativeLayout>
il suffit de faire attention aux éléments qui se superposent à d'autres éléments.
utiliser android: gravity= "start
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Has Diploma:"
android:textSize="15dp"
android:id="@+id/checkBox_HasDiploma"
android:gravity="start"/>
vous supprimez aussi android: orientation= "vertical" puisque la disposition Relative ne supporte pas cet attribut. C'est l'attribut de la disposition linéaire.
Vous pouvez l'essayer
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="8dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="8dip" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="check"
/>
<CheckBox
android:id="@+id/chekcbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
</LinearLayout>
il suffit de régler l'interrupteur: Paramètres de Layout - > Width to" match_parent "
à l'origine c'est "wrap_content "
vous pouvez y parvenir en ajoutant layoutDirection à "rtl"
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center"
android:layoutDirection="rtl"
android:text="@string/declare_info_correct" />