2010-06-25 4 views

Répondre

9

Je ne pense pas que vous le pouvez. En regardant le source code, il semble que la coche est dessinée sur la droite tout le temps. Et, pour être honnête, je vous recommande de rester avec cela, par souci de cohérence - Android est frappé tout le temps parce que ses applications ont des IU incohérentes.

Au hasard que quelqu'un pointe une arme sur votre tête, vous forçant à changer la mise en place de la coche, la sous-classe CheckedTextView comme OMGPleaseDontShootCheckedTextView, et remplacer les méthodes comme onDraw(), avec les versions tordu de l'original qui change la mise en place de l'image et le texte.

+0

Merci Ceci est utile ! –

+7

Maintenant, avec les coches de conception de matériel doivent être sur la gauche, mais le code pour permettre cela est malheureusement privé, donc ne peut pas être facilement rétroporté. –

28

Créez votre propre modèle de ligne et définissez android: drawableLeft sur CheckedTextView.

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="fill_parent" 
android:layout_height="40dip" 
android:gravity="center_vertical" 
android:paddingLeft="5dip" 
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
/> 

ou

android:drawableLeft="?android:attr/listChoiceIndicatorSingle" 
+2

Cela vous empêche d'utiliser la fonctionnalité de dessin composé avec l'indicateur de coche (que CheckedTextView permet par défaut). Je pense que la réponse de @CommonsWare est plus appropriée. –

-4

android: drawableLeft = "android: attr/listChoiceIndicatorMultiple": P

25

Le secret est dans Android: Checkmark rendent null

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/checked_text_single_choice" 
android:layout_width="match_parent" 
android:layout_height="52dp" 
android:checkMark="@null" 
android:gravity="center_vertical" 
android:textAppearance="?android:attr/textAppearanceSmall" 
android:drawableLeft="?android:attr/listChoiceIndicatorSingle" 
android:drawableRight="@null" 
android:textColor="@android:color/black" 
/> 
+2

Je ne peux pas croire que cela a fonctionné. merci – j2emanue

+0

solution de travail – vuhung3990

+1

quelle magie est-ce? ce n'est même pas logique, mais ça marche! – Ogbe

0

essayez celui-ci et espérez qu'il vous donnera quelques idées

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Connection lost sound alert" 
       android:layout_weight="10" 
       android:layout_gravity="center" 
      /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2"  
      android:layout_gravity="right" 
      android:orientation="horizontal" > 

      <CheckBox android:id="@+id/checkbox_meat" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right"     
       android:onClick="onCheckboxClicked"/> 

     </LinearLayout> 


    </LinearLayout> 
7

Si vous souhaitez que la case à cocher soit sur la gauche, utilisez simplement CheckBox. Peut-être que ce n'est pas grave, mais un CheckBox peut contenir du texte. Vous pouvez définir ce texte en ajoutant un attribut XML android:text ou en appelant la méthode setText(). En fait CheckBox est hérité de Button, qui hérite de TextView, c'est pourquoi il a toutes les propriétés liées au texte.

+0

ajouté exemple 'CheckBox' xml avec capture d'écran dans une réponse séparée –

2

Un exemple de suggestion de @ WonderCsabo pour utiliser CheckBox.

<CheckBox 
    android:id="@+id/create_account_checkedTextView_password" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Show" 
    android:gravity="center" 
    android:checked="true" 

    # relative layout params 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_above="@+id/hint1" /> 

checkbox_example

2

Il peut y avoir un truc avec Vérifiable mise en page comme dans ce post - https://chris.banes.me/2013/03/22/checkable-views/ . Il suffit d'utiliser une mise en page personnalisée comme celui-ci comme la mise en page de l'article ListView:

<com.example.custom_view.CheckableLinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 
<CheckedTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:duplicateParentState="true" 
    .../> 
<TextView 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    .../> 
</com.example.custom_view.CheckableLinearLayout> 

ou

android:checkMark=?android:attr/listChoiceIndicatorMultiple" 
2

Vous pouvez assigner votre étirables à l'attribut drawableLeft, mais vous ne ferez pas bon parce qu'il ne supporte teinter . Au lieu de cela, j'étendu CheckedTextView comme ça:

public class LeftSideCheckedTextView extends CheckedTextView { 

    public LeftSideCheckedTextView(Context context) { 
     this(context, null, 0); 
    } 

    public LeftSideCheckedTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public LeftSideCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     Field mCheckMarkGravity = null; 
     try { 
      mCheckMarkGravity = this.getClass().getSuperclass().getDeclaredField("mCheckMarkGravity"); 
      mCheckMarkGravity.setAccessible(true); 
      mCheckMarkGravity.set(this, Gravity.START); 
     } catch (Exception e) { 
      Logger.error(e); 
     } 
    } 
} 

Ici, j'accéder au mCheckMarkGravity champ caché qui est utilisé à l'intérieur CheckedTextView mais ont pas accès via des attributs de style, selon ce billet bug: https://code.google.com/p/android/issues/detail?id=174517

Questions connexes