左对齐文本,右对齐复选框。

50
我正在尝试为ListView创建布局,其中包括复选框和左侧的一些文本。复选框应该对齐到行的最右边,TextView对齐到行的最左边,例如:
 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------

这是我目前所拥有的:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:paddingLeft="5dip"
      android:paddingRight="5dip"
      android:paddingTop="8dip"
     android:paddingBottom="8dip"
>

<TextView  
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left" 
/>

<CheckBox 
    android:id="@+id/chekcbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right" 
 /> 

 </RelativeLayout>

然而,实际呈现的是文本框覆盖在复选框上方,在行的左侧。


可能是重复的问题:如何在Android中将复选框显示在右侧? - Rishabh Srivastava
13个回答

48

为了将复选框的方框放置在右侧,在复选框属性中进行设置

android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

1
先生,您刚刚救了我的命。我一直在努力寻找默认复选框图像,我差点就要使用appcompat了。对于“?”的使用加1。 - Barışcan Kayaoğlu
3
这将把 CheckBox 放在右侧,但当您具有属性 android:button="@null" 时,使用 colorAccent 进行 AppCompat 组件着色将不起作用。如何获得在右侧的 CheckBox 的组件着色? - Etienne Lawlor
我得到的最佳答案 - icgoogo

30

由于您已经在使用RelativeLayout,请利用它的属性:

从子项中删除android:gravity,并将其替换为android:layout_alignParentLeft="true"(TextView)和android:layout_alignParentRight="true"(CheckBox)。

这将相对于父元素边框定位子元素。您还可以为每个子元素添加android:layout_centerVertical="true"以使每个列表项内的两个垂直居中。

有关更多选项和属性,请参见文档


最好在TextView中添加android:layout_marginRight="36dp",如果您的文本太长,它将覆盖复选框。 - Hiep
1
嗨,如果我正在使用线性布局!!我仍然可以实现复选框复合按钮在右侧,而文本在左侧吗? - Shail Adi

13

首次使用时,您必须将button设置为@null

android:button="@null"

如果你想把Android复选框移到右边,可以使用以下代码:

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

否则,如果您想为复选框使用自定义图像,请使用:

android:drawableRight="@drawable/selector_check_box"

设置重力方向为右侧:

android:gravity="right|center_vertical"

如何完整地使用自定义复选框:

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
    <item android:state_window_focused="false" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_on_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/_ics_btn_check_off_pressed" />
    <item android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off" />
    <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on" />
    <item android:state_window_focused="false" android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
    <item android:state_window_focused="false" android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
    <item android:state_checked="false" android:drawable="@drawable/_ics_btn_check_off_disabled" />
    <item android:state_checked="true" android:drawable="@drawable/_ics_btn_check_on_disabled" />
</selector>

图片:

输入图像描述 输入图像描述 输入图像描述 输入图像描述 输入图像描述


12

在这种情况下,我的解决方案是使用:

<CheckBox
...
    android:button="@null"
    android:drawableRight="@drawable/my_check"
...
/>

其中my_radio可以是您自定义的选择器!选择器的示例可能是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/check_checked" />
    <item android:state_checked="false" android:drawable="@drawable/check_unchecked" />
</selector>

这样可以将 CheckBox 放在右侧,但是当你使用属性 android:button="@null" 时,使用 colorAccent 进行 AppCompat 小部件着色不起作用。如何在 CheckBox 在右侧的情况下实现小部件着色? - Etienne Lawlor

6
请使用以下代码作为列表/回收项。 以下两行是关键。 android:textDirection="ltr" android:layoutDirection="rtl"
<CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:textDirection="ltr"
            android:layoutDirection="rtl"
            />

如果Locale更改为RTL,视图会发生什么变化? - nitinkumarp
这是一个非常有价值的观点,目前我还没有检查过,如果您发现了什么,请检查并更新答案。 - Sagar Devanga
我已经使用了CheckedTextView来实现我的目的。 - nitinkumarp

5
基于Oibaf或MSaudi的解决方案
android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"

添加

android:paddingLeft="0dp"

为了避免在某些设备上复选框左侧出现空白区域。来源链接:

如何在Android中将复选框显示在右侧?

由zeusboy回答。


这样可以将 CheckBox 放在右侧,但是当你使用属性 android:button="@null" 时,使用 colorAccent 进行 AppCompat 小部件着色不起作用。如何在将 CheckBox 放在右侧的情况下实现小部件着色? - Etienne Lawlor
回复toobsco42:抱歉,我没有遇到你提到的场景。也许你可以创建一个带有布局文件和代码细节的问题,有人会给出解决方案。 - cfh008

3
您可以通过将layoutDirection设置为"rtl"来实现这一点。
<CheckBox
            android:id="@+id/checkbox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right|center"
            android:layoutDirection="rtl"
            android:text="@string/declare_info_correct" />

2

经过自己尝试,我终于做到了。您只需要在水平布局中放置一个TextView和一个CheckBox,并将布局的对齐方式设置为右侧即可。

以下是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:gravity="right"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
        >
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/LabelText"
        />
<CheckBox
        android:id="@+id/ChecKBox_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

编辑:我刚刚注意到你想把文本放在屏幕的最左边。这将把复选框放在最右边,文本紧挨着它放在复选框的左侧。

就像这样:

------------------------
           text checkbox
------------------------
           text checkbox
------------------------
           text checkbox
------------------------

1

使用 match_parent 作为 layout_widthandroid:layoutDirection="rtl" 就足够了:

<android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/taxDevRadioButton"
        android:layout_width="match_parent"
        android:layoutDirection="rtl"
        android:layout_height="wrap_content"
        android:layout_below="@+id/divider2"
        android:layout_marginTop="10dp"
        android:text="ELIGIBLE FOR TAX" />

1
android:layoutDirection="rtl" 已经生效,谢谢! - biinui

0

您还需要删除android:orientation="vertical",因为相对布局不支持此属性。这是线性布局的属性。

您可以尝试一下

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="8dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:paddingTop="8dip" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="check"
             />

        <CheckBox
            android:id="@+id/chekcbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" />
    </LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接