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

后端 未结 11 2368
悲哀的现实
悲哀的现实 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:18

    You can assign your drawable to drawableLeft attribute, but it won't make you any good because it not support tinting. Instead I extended CheckedTextView like that:

    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);
            }
        }
    }
    

    Here I access the hidden field mCheckMarkGravity which is used inside CheckedTextView but have no access via style attributes, according to this bug ticket: https://code.google.com/p/android/issues/detail?id=174517

提交回复
热议问题