【问题标题】:How to change background of Android checkBox?如何更改Android复选框的背景?
【发布时间】:2017-06-15 06:42:03
【问题描述】:

我有android checkBox,默认背景是透明的,我希望它是白色的,所以我使用样式:

<style name="BrandedCheckBox" parent="AppTheme">
    <item name="colorAccent">@color/cyan</item>
    <item name="colorControlNormal">@color/text_gray</item>
    <item name="colorControlActivated">@color/cyan</item>
</style>

并设置复选框主题:

<CheckBox
    android:id="@+id/check_payable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_gravity="center"
    android:theme="@style/BrandedCheckBox"/>

但结果是这样的: 但我希望它是这样的:

谁能帮我解决这个问题?

【问题讨论】:

标签: android xml checkbox


【解决方案1】:

你可以使用 android:button

   <CheckBox
       android:id="@+id/check_box"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:button="@drawable/checkbox_background" />

checkbox_background.xml

<?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/checkbox_checked" />
<item android:state_checked="false"
    android:drawable="@drawable/checkbox_unchecked" />
</selector>

我在drawable中使用这两个图像

【讨论】:

  • 我试过了,但我想保留默认的 checkbox_checked 我该怎么办?
  • 你可以在 Xml 中使用 android:checked="true"
  • 你能解释更多吗?你指的xml在哪里?
  • 我不想将默认状态设置为已检查我希望在检查时使用 android 的默认可绘制对象: 我应该如何改变这个?
【解决方案2】:

只需使用一个选择器可绘制对象,为每个状态定义您想要的确切外观(如果您需要,选择器中列出的可绘制对象可以具有透明度)。 然后还要确保在复选框的 xml 中进行设置:

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

为了避免 AppCompat Checkbox 最近出现的一些错误,我建议您坚持使用旧的 android.widget.CheckBox(您应该使用完整路径以确保您不会使用 AppCompat 之一)。

在不久的将来,您将能够从 androidX 迁移到 AppCompatCheckBox。然而此时(在我写这篇文章的时候)它在旧的 api 级别运行时仍然存在自定义背景的错误。我这里已经报了一个:https://issuetracker.google.com/issues/120865686

希望我的回答对你有用。

【讨论】:

    【解决方案3】:
    white color code #FFFFFF in your color.xml
    
    <style name="BrandedCheckBox" parent="AppTheme">
        <item name="colorAccent">@color/cyan</item>
        <item name="colorControlNormal">@color/text_gray</item>
        <item name="colorControlActivated">@color/white</item>
    </style>
    

    【讨论】:

      【解决方案4】:

      您可以使用代码:android:background="#006C84"

      <CheckBox
          android:id="@+id/cb"
          android:background="#006C84"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
      

      在活动中你可以使用:

      chkBox = (CheckBox)findViewById(R.id.cb);
      
      chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
      {
          @Override
          public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
          {
              // TODO Auto-generated method stub
              if (buttonView.isChecked())
              {
                  // When Checked
                  //cb.setBackgroundColor(Color.BLACK);
                  chkBox.setBackgroundColor(Color.parseColor("#FF8D3F"));
              }
              else
              {
                  // When Not Checked
                  // Set Your Default Color.
                  chkBox.setBackgroundColor(Color.parseColor("#006C84"));
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-19
        • 1970-01-01
        • 2012-10-05
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-27
        相关资源
        最近更新 更多