java - 在java中以编程方式设置android复选框的属性

标签 java android android-studio

在我的应用程序中,我在运行时创建了 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/

相关文章:

java - servlet netbeans mysql 连接

android - 适用于 Android 的 Google Analytics API 和使用 Tracker#send

java - 如何检查项目和导入模块的android插件版本是否相同?

android - 如何在 Android Studio 中解决 ClassNotFoundException

java - JavaFX 中 tableview 单元格中的 ComboBox

java - Android 上的 onActivityResult 方法内的全局变量值未更改

java - Autowiring 在 junit 测试中不起作用

java - canvas.drawCircle 不显示在 for 循环中

android - 从 .db 文件保存信息的正确方法是什么?

android - Intellij - 需要参数的android项目的gradle刷新