左对齐文本,右键对齐

Kri*_*s B 42 android android-layout

我正在尝试为ListView创建一个布局,右边有一个复选框,左边有一些文本.复选框应该一直向右对齐,TextView一直对齐到行的左边,例如:

 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------
 text            checkbox
 ------------------------
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止:

 <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>
Run Code Online (Sandbox Code Playgroud)

但是,实际渲染的是TextBox将复选框覆盖在行的左侧.

Com*_*are 41

使用CheckedTextView为你上面显示一切的替代品.

  • 要获得"原生复选框"样式,请使用`android:checkMark ="?android:attr/listChoiceIndicatorMultiple"` (6认同)
  • 使用[`android.R.layout.simple_list_item_multiple_choice`]再次变得更容易(http://alvinalexander.com/java/jwarehouse/android-examples/platforms/android-2/data/res/layout/simple_list_item_multiple_choice.xml.shtml )布局. (2认同)

MSa*_*udi 39

并在复选框属性中选中右侧的复选框

android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Run Code Online (Sandbox Code Playgroud)

  • 因此,这会将`CheckBox`置于右侧,但当您拥有属性`android:button ="@ null"`时,使用`colorAccent`的`AppCompat`小部件着色不起作用.如何使用右侧的"CheckBox"获取小部件? (3认同)

小智 30

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

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

这使孩子们相对于父母的边界.您可能还希望将android:layout_centerVertical="true"每个子项添加到每个列表项中的两个垂直中心.

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


Dol*_*rma 13

第一次你必须设置button@null

android:button="@null"
Run Code Online (Sandbox Code Playgroud)

如果你想移动android复选框正确使用:

android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Run Code Online (Sandbox Code Playgroud)

否则,如果您希望使用自定义图像以供复选框使用:

android:drawableRight="@drawable/selector_check_box"
Run Code Online (Sandbox Code Playgroud)

并将重力设定为右:

android:gravity="right|center_vertical"
Run Code Online (Sandbox Code Playgroud)

完整操作使用自定义复选框:

<?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>
Run Code Online (Sandbox Code Playgroud)

图片 :

在此输入图像描述 在此输入图像描述 在此输入图像描述 在此输入图像描述 在此输入图像描述


小智 11

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

<CheckBox
...
    android:button="@null"
    android:drawableRight="@drawable/my_check"
...
/>
Run Code Online (Sandbox Code Playgroud)

其中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>
Run Code Online (Sandbox Code Playgroud)


cfh*_*008 5

在Oibaf的基础上,它是MSaudi的解决方案

android:button="@null"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
Run Code Online (Sandbox Code Playgroud)

加上

android:paddingLeft="0dp"
Run Code Online (Sandbox Code Playgroud)

避免某些设备左侧复选框的空白区域.源链接:

如何在右侧显示android复选框?

由zeusboy回答.


Sag*_*nga 5

将此作为您的LIST/RECYCLER项目.以下2行是关键.

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"
            />
Run Code Online (Sandbox Code Playgroud)