//动态添加布局 // set 文本大小 LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(mBinding.llButtonWeight.getWidth()/result.size(), ViewGroup.LayoutParams.WRAP_CONTENT); //set 四周距离 params1.setMargins(0, 0, 0, 0); params1.weight=1; final RadioButton[] radioButton=new RadioButton[result.size()]; for(int i=0;i<result.size();i++){ if(result.get(i).getCode().equals(mType)){ radioButton[Count]=new RadioButton(mObj); radioButton[Count].setText(result.get(i).getSpecifications()); radioButton[Count].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int position = (int) buttonView.getTag(); if (isChecked) { radioButton[position].setTextColor(Color.WHITE); } else { radioButton[position].setTextColor(Color.BLACK); } } }); radioButton[Count].setTextColor(Color.BLACK); radioButton[Count].setTextSize(13); radioButton[Count].setGravity(Gravity.CENTER_HORIZONTAL); radioButton[Count].setButtonDrawable(null); radioButton[Count].setLayoutParams(params1); radioButton[Count].setTag(i); Count++; } } for(int j=0;j<Count;j++){ if(j==0){ radioButton[j].setBackgroundResource(R.drawable.left_specification_shape); }else if(j==Count-1){ radioButton[j].setBackgroundResource(R.drawable.right_specification_shape); }else { radioButton[j].setBackgroundResource(R.drawable.common_specification_shape); } mBinding.llButtonWeight.addView(radioButton[j]); mBinding.llButtonWeight.check(radioButton[0].getId()); } mBinding.llButtonWeight.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (ValueList.size() > 0) { for (PositionValue item : ValueList) { if (mType.equals(item.getCode())) { RadioButton rb= (RadioButton)group.findViewById(checkedId); int TagPosition = (int)rb.getTag(); item.setPosition(TagPosition); position=TagPosition; mBinding.advanceChargeShow.setText(result.get(TagPosition).getPrice()+"元"); mBinding.differencePriceShow.setText(VerifyUtil.trimTwoBit(result.get(TagPosition).getFuctuateAmount())+"元"); mBinding.serviceChargeShow.setText(VerifyUtil.trimTwoBit(result.get(TagPosition).getSellCommissionAmount())); mBinding.storagePriceShow.setText(VerifyUtil.trimTwoBit(result.get(TagPosition).getStorageCommissionAmount())+"元/天"); } } }
动态生成radiobutton
最新推荐文章于 2023-03-31 17:21:51 发布