[英]spacing for checkmark image of the check box in android
嗨,我正在尝试创建一个复选框列表,它应该看起来像下面的图像,它将在复选标记图像之前和之后有空格。
但当我试图创建复选框时,它显示如下图像。
检查标记图像没有空间环绕,就像我正在显示第一张图像。 如果我使用填充来复选框组件,它像“android:padding = 50dp”,它在复选标记图像和文本之间给出空格。 但是在Check-Mark图像之前没有在起始.ie中给出空间。 下面是我的xml布局复选框
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dp">
<CheckBox android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:button="@drawable/btn_check_square"
android:background="@drawable/filter_item_background"
android:textColor="@android:color/black"
android:padding="5dp"
android:text="check box text"/>
</LinearLayout>
任何帮助解决这个问题的人都表示赞赏。
这可以通过以下代码解决。
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/filter_item_background"
android:button="@android:color/transparent" //Make button null
android:drawableLeft="@drawable/btn_check_square" //Create button in drawable
android:drawablePadding="20dp" //Set space between Text & CB
android:padding="5dp"
android:text="check box text"
android:textColor="@android:color/black" />
试试这个(没有自定义图形):
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@android:color/transparent"
android:drawablePadding="8dp"
android:text="CheckBox" />
它使用了一个小技巧:由于Checkbox是从Button派生的,我们可以添加一个drawable,现在可以在label和drawable之间设置一个填充。 由于我们不需要真正的图标,我们使用透明颜色。
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/filter_item_background"
android:button="@null" //Make button null
android:drawableLeft="@drawable/btn_check_square" //Create button in drawable
android:drawablePadding="20dp" //Set space between Text & CB
android:padding="5dp"
android:text="check box text"
android:textColor="@android:color/black" />
使用以下代码
final float scale = this.getResources().getDisplayMetrics().density;
checkbox.setPadding(checkbox.getPaddingLeft()
+ (int) (10.0f * scale + 0.5f),
checkbox.getPaddingTop(),
checkbox.getPaddingRight(),
checkbox.getPaddingBottom());
将其添加到您的复选框:
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.