【问题标题】:Set size of checkbox's drawable设置复选框可绘制的大小
【发布时间】: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


    【解决方案1】:

    大多数人喜欢使用 android:background 之类的

    android:button="@null"
    android:background="@drawable/check_box"
    

    如果您仍然喜欢使用按钮,则将图像大小调整为 32dp。因为 android Material Design 建议对复选框使用 32dp。

    参考链接。可能会对你有所帮助。

    Material Guidelines

    【讨论】:

    • 如何在这样的复选框上实现阴影效果?属性 elevation 似乎不起作用。
    • @michalsol 实际上这是一张图片。所以你不能通过代码给阴影。但在将图像粘贴到可绘制图像之前,您可以通过外部照片编辑器对其进行更改
    猜你喜欢
    • 2015-07-13
    • 2011-07-04
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2013-03-08
    • 2022-06-21
    相关资源
    最近更新 更多