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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3114745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:46:17  来源:igfitidea点击:

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

android

提问by Kevin W

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 be left aligned instead of right aligned?

我正在尝试将 R.layout.simple_list_item_multiple_choice 与 ListView 一起使用。在 simple_list_item_multiple_choice.xml 中使用了 CheckedTextView,但是如何使复选框左对齐而不是右对齐?

回答by Mohammed Elsabry

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

回答by Roger Keays

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"

回答by CommonsWare

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 CheckedTextViewas OMGPleaseDontShootCheckedTextView, and override methods like onDraw(), with tweaked versions of the original that changes the placement of the image and text.

万一有人用枪指着你的头,迫使你改变复选标记的位置,子类CheckedTextViewOMGPleaseDontShootCheckedTextView,并覆盖诸如 的方法onDraw(),使用改变图像和文本位置的原始版本的调整版本。

回答by WonderCsabo

If you want the checkbox to be on the left, simply just use a CheckBox. Maybe it is not matter of course, but a CheckBoxcan contain text. You can define that text by adding an XML attribute android:text, or by calling the setText()method. Actually CheckBoxis inherited from Button, which inherits from TextView, that's why it has all the text-related properties.

如果您希望复选框位于左侧,只需使用CheckBox. 也许这当然无关紧要,但 aCheckBox可以包含文本。您可以通过添加 XML 属性android:text或调用setText()方法来定义该文本。实际上CheckBox是继承自Button,它继承自TextView,这就是为什么它具有所有与文本相关的属性。

回答by gorodechnyj

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:

您可以将 drawable 分配给 drawableLeft 属性,但它不会对您有任何好处,因为它不支持着色。相反,我像这样扩展了 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

回答by Stan Kurdziel

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

checkbox_example

回答by Roman_D

There can be a trick with Checkable Layout like in this post - https://chris.banes.me/2013/03/22/checkable-views/. Just use some custom layout like this as ListView item layout:

Checkable Layout 可能有一个技巧,例如这篇文章 - https://chris.banes.me/2013/03/22/checkable-views/。只需使用一些像这样的自定义布局作为 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"

回答by ksu

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>

回答by Prateek

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 - 复选框/单选按钮排列

回答by brybry



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

        }
    }
}