问题描述
我在这里有一个小问题。我使用android studio 3.6并实现了androidx库。 最低API级别16。 我需要更改radioButton的颜色,所以我使用了它:
AppCompaTradioButton rb = new AppCompaTradioButton(context);
rb.setSupportButtonTintList(colorStateList);
当我调用setSupportButtonTintList方法时,IDE会显示:
只能从相同的库组前缀(引用的groupId = androidx.appcompat和来自groupId = MyAppName的前缀androidx)内调用AppCompaTradioButton.setSupportButtonTintList
解决方法
根本原因:来自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)