在我的应用程序中,我在运行时创建了 10 个复选框,并且我想以编程方式设置以下 xml 属性。我一直在到处搜索,但找不到任何解决方案,而且我对 Android 开发还很陌生。将不胜感激任何形式的帮助!
android:button="@null"
android:drawableTop="?android:attr/listChoiceIndicatorMultiple"
我一直在研究如何访问 android:attr 文件,但没有成功。
最佳答案
您可以使用setButtonDrawable
来分配android:button="@null"
CheckBox checkBox = findViewById(R.id.checkbox);
checkBox.setButtonDrawable(null);
您可以使用setCompoundDrawablesRelativeWithIntrinsicBounds()
方法来分配可绘制对象。使用此代码获取 ?android:attr/listChoiceIndicatorMultiple
的值:
TypedValue typeValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple,typeValue, true);
checkBox.setCompoundDrawablesRelativeWithIntrinsicBounds(0,typeValue.resourceId,0,0);
关于java - 在java中以编程方式设置android复选框的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58310368/