mercredi 5 juillet 2017

Set size of checkbox's drawable

I would like to use custom drawables set as my checkbox drawable I have it defined as follows:

heart_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">
    <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 here are just .png files. In layout however I have a following entry:

    <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"/>

What I want to achieve is to set width and height of that CheckBox and has its button drawable scalled to fill it. Now it get cropped (when I use too big .png file) or stays small (when I use lower-resoultion .png). I believe dp units were introduced to prevent various items size depending on phone density. This is way I don't want to just use image with another resolution.

I have tried several ways to achieve what I want - without success. I've tried setting android:scaleType="fitCenter", android:gravity="center", I tried to change items in selector to introduce bitmaps there with android:scaleType="fitCenter" attribute. What seemed to work was setting that drawable to android:background attribute instead of to android:button. But I don't think this a right way (It can make some probles with shadows, elevation, etc. I think).

How do you recommend to achieve what I want?




Aucun commentaire:

Enregistrer un commentaire