左alignment文本,右边的checkbox
我正在尝试创build一个ListView的布局,右边的checkbox和左边的一些文本。 checkbox应该一直向右alignment,并且TextView一直alignment到行的左侧,例如:
------------------------ 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> 
但是,实际呈现的是TextBox覆盖行左侧的checkbox。
由于您已经在使用RelativeLayout,请使用它的属性:
 从子项中删除android:gravity ,并将其replace为TextView的android:layout_alignParentRight="true"和CheckBox的android:layout_alignParentRight="true" 。 
 这使孩子相对于父母的边界。 您可能还需要将android:layout_centerVertical="true"到每个子项中,以将每个列表项中的两个垂直居中。 
有关更多选项和属性,请参阅文档 。
 使用CheckedTextView代替上面显示的所有内容。 
并在checkbox的属性框右边的checkbox
 android:button="@null" android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 
 首次您必须将button设置为@null 
 android:button="@null" 
如果你想android的checkbox右移使用:
 android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 
否则,如果你喜欢有checkbox的自定义图像使用:
 android:drawableRight="@drawable/selector_check_box" 
并将重力设置为右:
 android:gravity="right|center_vertical" 
完全动作使用自定义checkbox:
 <?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> 
图片 :





我在这种情况下的解决scheme是使用:
 <CheckBox ... android:button="@null" android:drawableRight="@drawable/my_check" ... /> 
其中my_radio可能是你自定义select器! select器的例子可以是:
 <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> 
在Oibaf的基础上,或者MSaudi的解决scheme
 android:button="@null" android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 
加上
 android:paddingLeft="0dp" 
以避免某些设备左侧checkbox的空白。 来源链接:
如何在右侧显示androidcheckbox?
由zeusboy回答。
自己尝试完成后,我终于做到了。 你只需要把一个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> 
编辑:我只是注意到,你想要的文字一直到屏幕的左侧。 这将把checkbox一直放在右边,checkbox左边的文本就放在旁边。
喜欢这个:
 ------------------------ text checkbox ------------------------ text checkbox ------------------------ text checkbox ------------------------ 
使用android:gravity =“start”
  <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Has Diploma:" android:textSize="15dp" android:id="@+id/checkBox_HasDiploma" android:gravity="start"/> 
您还可以移除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> 
只需将开关:布局参数 – >宽度设置为“match_parent”
原来是“wrap_content”
如果你打算使用一个relativelayout,你可能要考虑使用较less讨厌的relativelayout属性,alignParentLeft和alignParentRight,如下所示:
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" 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:alignParentLeft="true" /> <CheckBox android:id="@+id/chekcbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alignParentRight="true" /> </RelativeLayout> 
只要注意元素越来越其他元素。
- Android图标与徽标
- BroadcastReceiver:不能实例化类; 没有空的构造函数
- 避免传递null作为视图根(需要parsing膨胀布局的根元素上的布局参数)
- 以编程方式replaceselect器图像
- Android Studio Gradle问题升级到版本0.5.0 – Gradle从0.8迁移到0.9 – Android Studio升级到0.8.1
- 如何从资源中设置位图
- 如何在Android 4.2中更改Action Bar的选项菜单的背景颜色?
- ScrollView内的Google Maps API v2 SupportMapFragment – 用户不能垂直滚动地图
- Facebook的“Messenger”有一个SMS广播接收器,在重启后具有最高优先级