简体   繁体   English

如何使 Android CheckedTextView 中的复选框左对齐而不是右对齐?

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

I am trying to use R.layout.simple_list_item_multiple_choice with ListView.我正在尝试将 R.layout.simple_list_item_multiple_choice 与 ListView 一起使用。 CheckedTextView is used in simple_list_item_multiple_choice.xml, but how can I make the checkbox be left aligned instead of right aligned?在 simple_list_item_multiple_choice.xml 中使用了 CheckedTextView,但是如何使复选框左对齐而不是右对齐?

The secret is in android:checkMark make it null秘诀在于 android:checkMark 使其为空

<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"
/>

Create your own row template and set android:drawableLeft on the CheckedTextView.创建您自己的行模板并在 CheckedTextView 上设置 android:drawableLeft。

<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"
/>

or或者

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

If you want the checkbox to be on the left, simply just use a CheckBox .如果您希望复选框位于左侧,只需使用CheckBox Maybe it is not matter of course, but a CheckBox can contain text.也许这当然无关紧要,但CheckBox可以包含文本。 You can define that text by adding an XML attribute android:text , or by calling the setText() method.您可以通过添加 XML 属性android:text或调用setText()方法来定义该文本。 Actually CheckBox is inherited from Button , which inherits from TextView , that's why it has all the text-related properties.实际上CheckBox继承自Button ,后者继承自TextView ,这就是为什么它具有所有与文本相关的属性。

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.而且,老实说,为了一致性,我建议你坚持下去——Android 一直受到打击,因为它的应用程序具有不一致的用户界面。

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. CheckedTextView有人用枪指着你的头,迫使你改变复选标记的位置,将CheckedTextView子类CheckedTextView OMGPleaseDontShootCheckedTextView ,并覆盖像onDraw()这样的方法,使用改变图像位置的原始版本的调整版本和文字。

You can assign your drawable to drawableLeft attribute, but it won't make you any good because it not support tinting.您可以将 drawable 分配给 drawableLeft 属性,但它不会对您有任何好处,因为它不支持着色。 Instead I extended CheckedTextView like that:相反,我像这样扩展了 CheckedTextView:

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在这里,我访问隐藏字段 mCheckMarkGravity,它在 CheckedTextView 中使用,但无法通过样式属性访问,根据此错误票证: https : //code.google.com/p/android/issues/detail? id=174517

An example of @WonderCsabo's suggestion to use CheckBox. @WonderCsabo 建议使用 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

There can be a trick with Checkable Layout like in this post - https://chris.banes.me/2013/03/22/checkable-views/ . Checkable Layout 可能有一个技巧,比如这篇文章 - https://chris.banes.me/2013/03/22/checkable-views/ Just use some custom layout like this as ListView item layout:只需使用一些像这样的自定义布局作为 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>

or或者

android:checkMark=?android:attr/listChoiceIndicatorMultiple"

Looking at the source, it's just based on layout direction.查看源代码,它只是基于布局方向。 Setting it to RTL works well enough for me.将它设置为 RTL 对我来说效果很好。

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

        }
    }
}

try this one and hope it will give you some ideas试试这个,希望它能给你一些想法

 <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>

If anyone is still looking for a solution without making a Checkbox in the layout.如果有人仍在寻找解决方案而不在布局中创建复选框。 Try the solution here.尝试这里的解决方案。

Android ListView ArrayAdapter - checkbox/radio button arrangement Android ListView ArrayAdapter - 复选框/单选按钮排列

添加此行使左侧的复选框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM