I want to display 6 radio buttons of same radio group. But all the 6 radio buttons remain on same line and goes off the screen. How to display them in two rows (3 radio buttons each)? I tried everything possible for me (I am new to android).
问题:
回答1:
From searching around, there doesn't appear to be a way of doing it, as RadioGroup uses LinearLayout, which does not wrap. As radio buttons must be direct children to the radio group, you can't add sub-layouts to radio group.
This means you will have to implement this layout behaviour manually. Two possible options are:
- Create a copy of RadioGroup to extend a different layout, or at least allow you control it dynamically.
- Implement your own custom layout to replace
RadioGroup
that extends a layout of your choice, and implementsOnClickListener
. There's a good example here.
回答2:
A simple answer would be to wrap your RadioGroup in a ScrollView so the user could scroll to the off-screen buttons (not real elegant, but not code intensive either).
回答3:
Dynamically generated by marginTop and marginLeft, to adjust the need to be in the same line of radiobutton. First, get the width of the screen, set the layoutparams marginLeft for the screen width of half, the height of the marginTop needs to be adjusted according to the specific radiobutton.eg:
holder.question = (TextView) convertView.findViewById(R.id.topic_item_question);
holder.option = (RadioGroup) convertView.findViewById(R.id.topic_item_option);
holder.option1 = (RadioButton) convertView.findViewById(R.id.topic_item_option1);
holder.option2 = (RadioButton) convertView.findViewById(R.id.topic_item_option2);
//为了能够在一行显示两个radiobutton:获取屏幕的宽度
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();//左侧设置的间距
int height = DensityDpToPx.dpToPx(context, 24);//处于第二个的高度间距
LinearLayout.LayoutParams params = (LayoutParams) holder.option2.getLayoutParams();
params.setMargins(width / 2, -height, 0, 0);
holder.option2.setLayoutParams(params);
holder.option3 = (RadioButton) convertView.findViewById(R.id.topic_item_option3);
holder.option4 = (RadioButton) convertView.findViewById(R.id.topic_item_option4);
LinearLayout.LayoutParams paramsTwo = (LayoutParams) holder.option4.getLayoutParams();
paramsTwo.setMargins(width / 2, -height, 0, 0);
holder.option4.setLayoutParams(paramsTwo);