2015-11-18 45 views
2

创建自定义的复选框绘制:自定义复选框的大小是否可绘制?

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

,我用这样的:

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_centerVertical="true" 
    android:button="@drawable/checkbox" 
    /> 

我创建了两个可绘制的图像(一个用于未选中,一个用于检查),每一个尺寸(xxxhdpixxhdpi等)。

然而,我的复选框出来是这样的:

enter image description here

为什么会出来这么大呢?我怎样才能让它变小到20x20dp?

+0

修复复选框的高度和宽度。 – Anjali

+0

给出代码ic_checkbox_unchecked。 –

+0

@ Uit14bs'ic_checkbox_unchecked'和'ic_checkbox_checked'是图像。 – user5495265

回答

4

而不是wrapcontent尝试

<CheckBox 
android:layout_width="20dp" 
android:layout_height="20dp" 
android:layout_alignParentEnd="true" 
android:layout_centerVertical="true" 
android:button="@drawable/checkbox" 
/> 

或者

<CheckBox 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentEnd="true" 
android:layout_centerVertical="true" 
android:button="@drawable/checkbox" 
android:scaleX="0.40" 
android:scaleY="0.40" 
/> 

更改刻度值,直到你达到你想要的size

+0

该复选框被截断(现在只有一条垂直线) :http://i.imgur.com/rqR70nf.png – user5495265

+0

@ user5495265或试着缩放它,再次检查编辑 –

+0

@ user5495265我希望现在一切都好了。 –

0

您可以使用xml创建这种类型的复选框,并获得您想要的结果。

将另一个布局文件名称设置为ic_checkbox_unchecked并将此文件设置为可绘制的图像。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners 
android:topLeftRadius="0dp" 
android:topRightRadius="0dp" 
android:bottomLeftRadius="0dp" 
android:bottomRightRadius="0dp" 
/> 
<solid 
android:color="#FFFFFC" 
/> 

<size 
android:width="25dp" 
android:height="25dp" 
/> 
<stroke 
android:width="3dp" 
android:color="#236C87" 
/> 

您可以通过使用<size />标签来填充宽度和高度。 您也可以使用<stroke />标签更改边框宽度。

相关问题