簡體   English   中英

如何使 Android CheckedTextView 中的復選框左對齊而不是右對齊?

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

我正在嘗試將 R.layout.simple_list_item_multiple_choice 與 ListView 一起使用。 在 simple_list_item_multiple_choice.xml 中使用了 CheckedTextView,但是如何使復選框左對齊而不是右對齊?

秘訣在於 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"
/>

創建您自己的行模板並在 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"
/>

或者

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

如果您希望復選框位於左側,只需使用CheckBox 也許這當然無關緊要,但CheckBox可以包含文本。 您可以通過添加 XML 屬性android:text或調用setText()方法來定義該文本。 實際上CheckBox繼承自Button ,后者繼承自TextView ,這就是為什么它具有所有與文本相關的屬性。

我不認為你可以。 源碼,好像一直在右邊畫勾。 而且,老實說,為了一致性,我建議你堅持下去——Android 一直受到打擊,因為它的應用程序具有不一致的用戶界面。

CheckedTextView有人用槍指着你的頭,迫使你改變復選標記的位置,將CheckedTextView子類CheckedTextView OMGPleaseDontShootCheckedTextView ,並覆蓋像onDraw()這樣的方法,使用改變圖像位置的原始版本的調整版本和文字。

您可以將 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);
        }
    }
}

在這里,我訪問隱藏字段 mCheckMarkGravity,它在 CheckedTextView 中使用,但無法通過樣式屬性訪問,根據此錯誤票證: https : //code.google.com/p/android/issues/detail? id=174517

@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

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>

或者

android:checkMark=?android:attr/listChoiceIndicatorMultiple"

查看源代碼,它只是基於布局方向。 將它設置為 RTL 對我來說效果很好。

android:layoutDirection="rtl"

來源:

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

        }
    }
}

試試這個,希望它能給你一些想法

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

如果有人仍在尋找解決方案而不在布局中創建復選框。 嘗試這里的解決方案。

Android ListView ArrayAdapter - 復選框/單選按鈕排列

添加此行使左側的復選框

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM