单选按钮多行

时间:2012-06-09 11:28:15

标签: android radio-button rows

我想显示相同广播组的6个单选按钮。但是所有6个单选按钮都保持在同一条线上并离开屏幕。如何以两行显示它们(每个3个单选按钮)? 我为我尝试了一切可能(我是android新手)。

3 个答案:

答案 0 :(得分:4)

从搜索开始,似乎没有办法,因为RadioGroup使用的是LinearLayout,它不会换行。由于单选按钮必须是直接子节点到无线电组,因此无法将子布局添加到无线电组。

这意味着您必须手动实现此布局行为。两种可能的选择是:

  • 创建RadioGroup的副本以扩展不同的布局,或者至少允许您动态控制它。
  • 实现您自己的自定义布局,以替换扩展您选择的布局的RadioGroup,并实现OnClickListener。有一个很好的例子here

答案 1 :(得分:1)

一个简单的答案是将您的RadioGroup包装在ScrollView中,这样用户就可以滚动到屏幕外按钮(不是真正优雅,但也不是代码密集)。

答案 2 :(得分:0)

由marginTop和marginLeft动态生成,以调整需要在radiobutton的同一行。首先,获取屏幕的宽度,设置layoutparams marginLeft为屏幕宽度的一半,marginTop的高度需要根据具体的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);
相关问题