作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想以编程方式将我动态创建的复选框设置为单选按钮,因为我喜欢这种外观。
为此,我创建了一个 checkboxStyle.xml 并将其放在 res 文件夹的值部分。
checkboxStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkboxStyle">
<item name="android:checkboxStyle">@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton</item>
</style>
</resources>
然后我在我的 fragment 中调用它,因为我创建了我的复选框,但是 R.id.checkboxStyle
正在出现,因为没有找到,但我不确定把这个 id 放在哪里或者我是否需要换种方式引用它
CheckBox checkBox;
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("this");
list.add("thing");
final ArrayList<CheckBox> checkBoxArray = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
checkBox.setId(i);
checkBox.setText(list.get(i));
checkBox.setTag(list.get(i));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.v("CheckBox - checked", buttonView.getTag().toString());
}else{
Log.v("CheckBox - unchecked", buttonView.getTag().toString());
}
}
});
checkBoxArray.add(checkBox);
checkboxContainer.addView(checkBox);
}
testing_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/checks"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
最佳答案
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
它不是一个id
,它是一个style
,你需要像这样访问它
R.style.checkboxStyle;
关于java - 找不到复选框的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762704/
我是一名优秀的程序员,十分优秀!