【问题标题】:Create custom check boxes in android在android中创建自定义复选框
【发布时间】:2011-06-15 05:51:54
【问题描述】:

我有一个安卓应用程序。有一个活动需要 7 个复选框,但由于 SDK 提供的默认复选框的大小非常大并且占用大量空间,我尝试开发自己的自定义复选框。

为此,我捕获了两张图像(选中和未选中),单击其中一个会切换,即单击选中时它会转换为未选中,反之亦然。

但我想知道有没有其他方法可以做到这一点..

【问题讨论】:

    标签: android customization


    【解决方案1】:

    通过为您的复选框状态放置一个适当的可绘制 xml 文件以及您需要设置的相应图像,这有点简单。

    将下面的代码放在名为 checkbox.xml 的 drawable 中

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
              android:drawable="@drawable/star_checked" /> <!-- pressed -->
        <item android:state_checked="false"
              android:drawable="@drawable/star_unchecked" /> <!-- focused -->
         <item android:drawable="@drawable/star_unchecked" />      
    </selector>
    

    并在上面的代码中设置复选框,通过使用 android:button=@drawable/checkbox"

    <CheckBox android:id="@+id/chkFav" android:layout_width="wrap_content"
            android:layout_marginRight="0dp" android:button="@drawable/checkbox"
                  android:layout_height="wrap_content" android:clickable="true"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2011-03-24
      • 1970-01-01
      • 2013-09-07
      • 1970-01-01
      • 2015-10-06
      相关资源
      最近更新 更多