自定义复选框图标的尺寸是多少?

3
我创建了一个自定义复选框drawable:
<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?


你正在使用 ic_checkbox_unchecked,它的尺寸是多少? - Shubhank Gupta
1
您在复选框中使用了“wrap_content”尺寸,可能是由于您的可绘制大小过大,因此显示了大的复选框。 - Shubhank Gupta
@ShubhankGupta 我已经为每个尺寸(xxxhdpixxhdpi 等)创建了一个可绘制的文件。复选框应该有多大? - user5495265
  1. 将ic_checkbox_unchecked.png在xxhdpi中的大小更改为4040,在xxxhdpi中更改为6060。2. 在代码中使用setButton。
- tiny sunlight
@user5495265 我认为你可以通过使用XML来创建ic_checkbox_unchecked图像。 - Syed Qasim Ahmed
显示剩余5条评论
2个回答

5

不要使用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"
/>

调整比例值,直到你达到所需的大小


复选框被切断了(只有一条竖线):http://i.imgur.com/rqR70nf.png - user5495265
@user5495265 或尝试缩放它,检查编辑。 - Collins Abitekaniza
@user5495265,我希望现在一切都好了。 - Collins Abitekaniza
那不过给我的并不是20x20dp。 - user5495265
@user5495265 那只是一个例子,请尝试减小比例值以获得您期望的尺寸。 - Collins Abitekaniza

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 /> 标签更改边框宽度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接