Preparing customized radio group type of layout

前端 未结 7 532
生来不讨喜
生来不讨喜 2020-12-30 08:49

I am trying to prepare custom radiogroup like layout in the below image. I have nearly 8-10 rows to do that. So, I prepared one linear layout w

7条回答
  •  礼貌的吻别
    2020-12-30 09:13

    In my experience is a little of a troublemaker to work with these kind of customized RadioGroups. I've prepared you some code that would be my approach to solve what you're trying to accomplish. Hope it works for you!

    First you must call this function in your onCreate() (or wherever you're creating the views)

     private void addRadioButtons() {
        LinearLayout llGroup = (LinearLayout) findViewById(R.id.linearLayoutGroup);
        for(int i=0; i<10; i++){
            MyRadioButton mrb = new MyRadioButton(this);
            mrb.setText(String.valueOf(i));
            llGroup.addView(mrb.getView());
        }
    }
    

    The class should be

    private static class MyRadioButton implements View.OnClickListener{
    
        private ImageView iv;
        private TextView tv;
        private RadioButton rb;
        private View view;
    
        public MyRadioButton(Context context) {
    
            view = View.inflate(context, R.layout.my_radio_button, null);
            rb = (RadioButton) view.findViewById(R.id.radioButton1);
            tv = (TextView) view.findViewById(R.id.textView1);
            iv = (ImageView) view.findViewById(R.id.imageView1);
    
            view.setOnClickListener(this);
            rb.setOnCheckedChangeListener(null);
    
        }
    
    
        public View getView() {
            return view;
        }
    
        @Override
        public void onClick(View v) {
    
            boolean nextState = !rb.isChecked();
    
            LinearLayout lGroup = (LinearLayout)view.getParent();
            if(lGroup != null){
                int child = lGroup.getChildCount();
                for(int i=0; i

    And the xml to inflate, something like:

    
    
        
        
        
    
    

提交回复
热议问题