简体   繁体   English

使用自定义复选框选择器时,如何消除复选框按钮和文本之间的多余空间?

[英]How can I get rid of extra space between the checkbox button and text when using a custom checkbox selector?

I have a very simple custom checkbox selector: 我有一个非常简单的自定义复选框选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:drawable="@drawable/ui_checkbox_checked" />
   <item android:drawable="@drawable/ui_checkbox" />
</selector>

that I'm setting using the following code: 我正在使用以下代码进行设置:

<CheckBox android:id="@+id/checkbox1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"
   android:button="@drawable/checkbox_selector"
   android:textSize="12dp"
   android:text="checkbox 1"
   />

The custom checkbox image is smaller than the standard image that comes with Android, but the text is still in exactly the same spot, as if the larger image were still being used. 自定义复选框图片比Android随附的标准图片小,但文本仍在同一位置,就好像仍在使用较大的图片一样。

So instead of looking like [X]_checkbox 1 it looks like [X]____checkbox 1 with a big space between the box and the text. 因此,看起来像[X]_checkbox 1而不是[X]____checkbox 1 ,在框和文本之间有很大的空间。 How can I fix this? 我怎样才能解决这个问题?

One potential solution is to set the checkBox to have no text. 一种可能的解决方案是将复选框设置为不包含文本。 Then make yourself a TextView and use android:layout_toRightOf="@+id/checkbox1" 然后使自己成为TextView并使用android:layout_toRightOf="@+id/checkbox1"

Probably is non-ideal since it means using an additional View, but this is how I've solved the problem that you currently face in the past. 可能不理想,因为这意味着要使用其他视图,但是这就是我解决了您过去遇到的问题的方式。

Size of control depends on foreground and background sizes. 控制的大小取决于前景和背景的大小。 When you customize checkbox - you make custom foreground, but background is left from android. 当您自定义复选框时-您可以自定义前景,但是背景是android留下的。 If your custom foreground is larger then android's background there is no problem. 如果您的自定义前景较大,则android的背景没有问题。 If you use smaller custom foregraund then size of checkbox is calculated from size of system background (which isn't good). 如果您使用较小的自定义前额,则复选框的大小是根据系统背景的大小来计算的(不好)。 Try to use small transparent .png as background: android:background="@drawable/small_bg" (or make experiments with some .9.png) 尝试使用小的透明.png作为背景:android:background =“ @ drawable / small_bg”(或使用.9.png进行实验)

You can set android:paddingLeft attribute to avoid extra space between image and text view Example 您可以设置android:paddingLeft属性,以避免图像和文本视图实例之间的额外空间

 <CheckBox android:id="@+id/checkbox1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"

   android:textSize="12dp"
   android:text="checkbox 1"
   android:paddingLeft="0dip"
   />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用自定义选择器时删除默认单选按钮复选框 - remove default radio button checkbox when using a custom selector 如何摆脱上载按钮和国家/地区文字输入之间的空间? - How can i get rid of the space between the upload button and the country textinput? GridView:如何摆脱GirdView对象的多余空间? - GridView: How can I get rid of extra space from my GirdView object? 如何摆脱按钮和其他视图之间的额外差距? - How to get rid of the extra gap between a button and other views? 如何删除复选框旁边的多余空间? - How to remove this extra space next to a checkbox? 无法从ListView CheckBox获取复选框文本? - Can not get checkbox text from ListView CheckBox? 在Android中以编程方式制作复选框时,如何从复选框中获取选中的复选框值 - How can i get checked checkboxes values from checkbox when checkbox made programmatically in android 如果我还向 EditText 添加文本,如何消除出现的 ImageSpan 上不需要的空间? - How can I get rid of unwanted space over ImageSpan that appears if I also add text to EditText? 我在适配器中创建了复选框和按钮动态,当我单击复选框时,我需要更改按钮中的文本颜色。 但我无法刷新视图 - I created checkbox and button dynamic in adapter, when i click checkbox i need to change Text Color in button. But i can't refresh the view 复选框按钮选择器导致隐藏复选框 - CheckBox button selector results in invisble checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM