How do I make the Checkbox in Android CheckedTextView be left aligned instead of right aligned?

后端 未结 11 2363
悲哀的现实
悲哀的现实 2020-12-09 14:54

I am trying to use R.layout.simple_list_item_multiple_choice with ListView. CheckedTextView is used in simple_list_item_multiple_choice.xml, but how can I make the checkbox

相关标签:
11条回答
  • 2020-12-09 15:21

    The secret is in android:checkMark make it 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"
    />
    
    0 讨论(0)
  • 2020-12-09 15:23

    An example of @WonderCsabo's suggestion to use 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

    0 讨论(0)
  • 2020-12-09 15:27

    I don't think you can. Looking at the source code, it seems like the checkmark is drawn on the right all the time. And, to be honest, I would recommend you stick with that, for consistency -- Android gets knocked all the time because its apps have inconsistent UIs.

    In the off chance that somebody is pointing a gun at your head, forcing you to change the placement of the checkmark, subclass CheckedTextView as OMGPleaseDontShootCheckedTextView, and override methods like onDraw(), with tweaked versions of the original that changes the placement of the image and text.

    0 讨论(0)
  • 2020-12-09 15:29

    Looking at the source, it's just based on layout direction. Setting it to RTL works well enough for me.

    android:layoutDirection="rtl"
    

    Source:

       private boolean isCheckMarkAtStart() {
        final int gravity = Gravity.getAbsoluteGravity(mCheckMarkGravity, getLayoutDirection());
        final int hgrav = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
        return hgrav == Gravity.LEFT;
    }
    

    .

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        ...
    
            final boolean checkMarkAtStart = isCheckMarkAtStart();
            final int width = getWidth();
            final int top = y;
            final int bottom = top + height;
            final int left;
            final int right;
            if (checkMarkAtStart) {
                left = mBasePadding;
                right = left + mCheckMarkWidth;
            } else {
                right = width - mBasePadding;
                left = right - mCheckMarkWidth;
            }
            ...
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:33

    Adding this line makes checkbox on left

     android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
    
    0 讨论(0)
提交回复
热议问题