【问题标题】:Removing the 'box' from the checkbox in Android从Android中的复选框中删除“框”
【发布时间】:2013-04-10 09:33:16
【问题描述】:

我是一名入门级软件开发人员,从这个网站上找到了很多很好的答案,但我似乎找不到如何在 Android 中隐藏复选框的“框”。当用户选择一个选项时,我只想显示复选标记。以下是我最近尝试过的一些事情。

chkSortOrder.setBackgroundResource(android.R.color.transparent); chkSortOrder.setBackgroundResource(android.R.drawable.checkbox_off_background);

这两个仍然显示该框。

【问题讨论】:

  • 谢谢,这个答案我遇到过几次,但在 Android 中找不到 setBorder 选项。
  • 我找不到任何东西,所以我只是使用一张支票的图像,要么显示要么不显示。 :^ \

标签: android checkbox


【解决方案1】:

将以下属性放在 XML 中的复选框标记中。

android:button="@android:color/transparent"

【讨论】:

  • 没问题 - 只需在您的代码中设置按钮属性。
  • 工作,但有点太好了。完整的复选框图像消失了。我认为原始海报只想显示 CHECK(在适当的情况下),而不显示框。
【解决方案2】:

现在已经很晚了,但无论如何,如果它对任何人都有帮助的话。如果您想以编程方式将自定义背景设置为 RadioButton,则可以这样设置,

checkbox.setButtonDrawable(null);
checkbox.setBackgroundResource(R.drawable.your_custom_drawable);

【讨论】:

  • 这似乎不适用于 Android 4.0。但是它在 5.0+ 中确实如此
【解决方案3】:

从 androidx.appcompat:appcompat:1.0.0 更新到 1.1.0 时发现此错误。

其他答案对我来说都没有用,因为将按钮属性设置为 @null 或 @android:color/transparent 在 Android API 级别 20 或更低版本上不起作用。

我所做的是将我的 CheckBox 更改为 ToggleButton 并将 textOn 和 textOff 属性设置为空字符串

<ToggleButton
    ...
    android:background="@drawable/custom_selector"
    android:textOff=""
    android:textOn="" />

这样我可以更新到 androidx appCompat 1.1.0

【讨论】:

  • 这是ToggleButton,而不是CheckBox(有onoff 状态)。
  • 问的问题是复选框而不是切换按钮。两者都不一样。
  • 在投反对票之前,请阅读漏洞答案并在有意义的情况下进行。我说我将 CheckBox 更改为 ToggleButton。如果您无法对任何不会使此答案不正确的业务要求进行更改。它只是不符合您的需求。
【解决方案4】:

您还可以在CheckBoxTextView 之间切换。只需使一个视图可见,另一个视图隐藏。这也是有道理的,因为CheckBox 在复选标记周围包含填充。

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >

            <CheckBox
                android:id="@+id/checkbox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checkMark="@null"
                android:checked="true"
                android:gravity="center_vertical"
                android:paddingStart="3dp"
                android:paddingLeft="3dp"
                android:text="My text"
                android:textColor="@color/black"
                android:textSize="12sp"
                />

            <TextView
                android:id="@+id/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="My text"
                android:textColor="@color/black"
                android:textSize="12sp"
                />

        </FrameLayout>

【讨论】:

    【解决方案5】:

    现在是 2020 年!许多事情都发生了变化。但是,如果您像我一样在这方面苦苦挣扎,并且在这里尝试了所有方法(和其他解决方案)却没有运气,那么也试试这个。我从其他解决方案中得到的就是这个(模拟器,API 16):

    奇怪的行为!两个drawable,右边是我的自定义框,左边是默认的。然后我不小心添加了这个:

    app:buttonCompat="@null"
    

    第二个终于消失了! 这是完整的定义:

      <CheckBox
            android:id="@+id/cb_fs_ExactSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:text="@string/fs_options_exact"
            android:textColor="@color/textPrimary"
            android:textSize="@dimen/textSmall"
            app:buttonCompat="@null"
            app:drawableRightCompat="@drawable/drw_checkbox" />
    

    您需要将buttonbuttonCompat 都设置为null。

    【讨论】:

    • 如果我只将按钮设置为 null 会发生什么?
    • @HaiHack 当你看到另一个复选框出现在左侧!
    • 感谢同胞!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2012-07-18
    • 2014-12-31
    相关资源
    最近更新 更多