【发布时间】:2017-07-05 17:05:26
【问题描述】:
我想使用自定义可绘制对象作为我的复选框可绘制对象,我将其定义如下:
heart_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/heart_outline" />
<item android:state_checked="true" android:drawable="@drawable/heart_full" />
<item android:drawable="@drawable/heart_outline" /> <!-- default state -->
</selector>
这里的drawables只是.png文件。但是在布局中,我有以下条目:
<CheckBox
android:id="@+id/heartItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:button="@drawable/favourite_heart_button"
android:contentDescription="@string/heartCheckBoxDesc"
android:focusable="false"/>
我想要实现的是设置 CheckBox 的宽度和高度,并使其按钮可绘制来填充它。现在它被裁剪(当我使用太大的 .png 文件时)或保持很小(当我使用较低分辨率的 .png 时)。
我相信引入dp 单位是为了防止因手机密度而异的物品尺寸。这是我不想只使用其他分辨率的图像的方式。
我尝试了几种方法来实现我想要的 - 没有成功。我尝试设置android:scaleType="fitCenter",android:gravity="center",我尝试在selector 中更改items 以在bitmaps 那里引入android:scaleType="fitCenter" 属性。似乎可行的是将drawable设置为android:background属性而不是android:button。但我认为这不是正确的方法(我认为它可能会产生一些阴影、高程等问题)。
你建议如何实现我想要的?
【问题讨论】:
标签: android checkbox android-drawable android-button android-checkbox