【问题标题】:Error in AppCompatRadioButton setSupportButtonTintList methodAppCompatRadioButton setSupportButtonTintList 方法中的错误
【发布时间】:2021-02-12 00:00:04
【问题描述】:

我有一个小问题。我使用 android studio 3.6 并实现了 androidx 库。 最低 API 级别 16。 我需要改变radioButton的颜色所以我用了这个:

  AppCompatRadioButton rb = new AppCompatRadioButton(context);
  rb.setSupportButtonTintList(colorStateList);

当我调用 setSupportButtonTintList 方法时,IDE 显示:

AppCompatRadioButton.setSupportButtonTintList 只能在同一个库组前缀内调用(引用 groupId=androidx.appcompat,前缀 androidx 来自 groupId=MyAppName)

【问题讨论】:

    标签: android android-appcompat androidx


    【解决方案1】:

    根本原因:来自AppCompatRadioButton源代码:

    /**
     * This should be accessed from {@link androidx.core.widget.CompoundButtonCompat}
     * @hide
     */
    @RestrictTo(LIBRARY_GROUP_PREFIX)
    @Override
    public void setSupportButtonTintList(@Nullable ColorStateList tint) {
        if (mCompoundButtonHelper != null) {
            mCompoundButtonHelper.setSupportButtonTintList(tint);
        }
    }
    

    这意味着从你的代码中调用这个方法,你有两个选择:

    • 通过CompoundButtonCompat访问
    • 从同一个库组前缀访问,在本例中为 androidx。但这个前缀是为 Android 保留的。

    解决方案:通过CompoundButtonCompat访问

    val rb = AppCompatRadioButton(context)
    CompoundButtonCompat.setButtonTintList(rb, colorStateList)
    

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多