Android RadioGroup 横向显示(两行两列)
2016-12-30 15:31
239 查看
做的android平板项目中,需要做一个调查问卷的界面,所以需要使用radiogroup的单选按钮,在listview的一个item上添加四个radiobutton,具体的demo是参考这位博主的,链接如下:http://blog.csdn.net/who0am0i/article/details/45200709,里面解决了我最大的难题就是如何在一个item中,识别用户点击的是哪一个radio,运行结果如下:
可以看到上方的radiobutton的排列方式是一行行的排布的,查找资料发现radiogroup默认继承的是 linearylayout,所以只能指定一个方向也就是竖向或者横向的布局,但如果放在平板里面,就需要显示成两行,我想当然的认为:看到这里我觉得在radiogroup里面在使用linearlayout,使用权重就可以实现一行两个radiobutton,平均分宽度,如下代码:
效果如图:
2.但我错了,radiogroup里面如果出现了其他的linearlayout,就会导致radiobutton所有的都会选中,不会自动取消别的radiobutton的选中状态,所以这种方式是行不通的,接着去谷歌了一下,发现他们的方案是采用两个radiogroup(参考链接:http://stackoverflow.com/questions/10425569/radiogroup-with-two-columns-which-have-ten-radiobuttons),这种方式没有尝试过,但无疑会增加逻辑上的难度,我在listview里面也无法判断选中的状态,
所以还是想别的方法,接着看到这个博主:http://blog.csdn.net/mrzhang_happy/article/details/48436105的文章,使用margin对第二个和第四个进行调整,使他们的间距刚好在同一行,不过这里有一个很严重的问题是:这些间距都是写死了的,如果换了一个手机屏幕,就会发生错位的现象、无法均分屏幕的宽度,之后想到这个宽度其实可以在生成布局之后,计算屏幕的宽度,然后动态的设置margin的大小,这样就可以解决不同分辨率下手机的错位问题,如下代码:
3. 布局文件还是上面的最简单的布局,横竖屏运行结果如下:
手机的运行结果:
这样就可以很好地解决radiogroup中的多行多列的显示,适配平板和多种尺寸手机的屏幕(附注:marginTop的高度需要根据你的显示的高度进行调试出一个大概的高度)
转换dp和px的工具类如下:
可以看到上方的radiobutton的排列方式是一行行的排布的,查找资料发现radiogroup默认继承的是 linearylayout,所以只能指定一个方向也就是竖向或者横向的布局,但如果放在平板里面,就需要显示成两行,我想当然的认为:看到这里我觉得在radiogroup里面在使用linearlayout,使用权重就可以实现一行两个radiobutton,平均分宽度,如下代码:
version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@color/white" android:orientation="vertical" > <TextView android:id="@+id/topic_item_question" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/title_bg" android:gravity="left" android:textColor="@color/white" android:textSize="@dimen/font_size_16" /> <RadioGroup android:id="@+id/topic_item_option" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="12dp" android:orientation="vertical" android:textSize="@dimen/font_size_16" > <LinearLayout android:id="@+id/top_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/topic_item_option1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="@dimen/font_size_16" /> <RadioButton android:id="@+id/topic_item_option2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="@dimen/font_size_16" /> </LinearLayout> <LinearLayout android:id="@+id/bottom_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/topic_item_option3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="@dimen/font_size_16" /> <RadioButton android:id="@+id/topic_item_option4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="@color/black" android:textSize="@dimen/font_size_16" /> </LinearLayout> </RadioGroup> </LinearLayout>
效果如图:
2.但我错了,radiogroup里面如果出现了其他的linearlayout,就会导致radiobutton所有的都会选中,不会自动取消别的radiobutton的选中状态,所以这种方式是行不通的,接着去谷歌了一下,发现他们的方案是采用两个radiogroup(参考链接:http://stackoverflow.com/questions/10425569/radiogroup-with-two-columns-which-have-ten-radiobuttons),这种方式没有尝试过,但无疑会增加逻辑上的难度,我在listview里面也无法判断选中的状态,
所以还是想别的方法,接着看到这个博主:http://blog.csdn.net/mrzhang_happy/article/details/48436105的文章,使用margin对第二个和第四个进行调整,使他们的间距刚好在同一行,不过这里有一个很严重的问题是:这些间距都是写死了的,如果换了一个手机屏幕,就会发生错位的现象、无法均分屏幕的宽度,之后想到这个宽度其实可以在生成布局之后,计算屏幕的宽度,然后动态的设置margin的大小,这样就可以解决不同分辨率下手机的错位问题,如下代码:
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, 34);//处于第二个的高度间距,这个高度需要自己进行调试,找到一个合适的高度 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);
3. 布局文件还是上面的最简单的布局,横竖屏运行结果如下:
手机的运行结果:
这样就可以很好地解决radiogroup中的多行多列的显示,适配平板和多种尺寸手机的屏幕(附注:marginTop的高度需要根据你的显示的高度进行调试出一个大概的高度)
转换dp和px的工具类如下:
package com.cxl.optiondemo; import android.content.Context; /** * 安卓手機dp和px之間的轉換 * * @author qiulinhe * @createTime 2016年7月15日 下午1:52:19 */ public class DensityDpToPx { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dpToPx(final Context context, final float dp) { return (int) (dp * context.getResources().getDisplayMetrics().density); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }
相关文章推荐
- Android RadioGroup 横向显示
- Android RadioGroup中横向、竖向布局RadioButton的问题
- Android 自定义View之RadioGroup实现跨多行显示
- Android RadioGroup中横向、竖向布局RadioButton的问题
- 让Android中RadioGroup不显示在输入法上面的办法
- Android 动态生成 radiobutton 解决 radiogroup 多行显示的问题 单选
- 【android】RadioGroup+RadioButton+HorizontalScrollview实现顶部横向菜单栏
- tabHost研究二 ,android应用中,大部分的软件都会采用把按钮放在底部显示(tabHost)。利用三个Radiobutton嵌套在RadioGroup 。
- Android RadioGroup多行显示,解决单选问题
- 【android基础学习之三】——基础控件Toast,EditText,RadioGroup,RadioButton
- Android PopupWindow RadioGroup RadioButton 的使用
- Android控件系列之RadioButton&RadioGroup
- 20、Android之RadioGroup
- Android控件系列之RadioButton&RadioGroup
- Android UI之RadioGroup、RadioButton组件
- android控件 RadioGroup 下拉菜单模式
- android控件07 RadioGroup
- 关于Android中的radioGroup选择
- android 手机开发笔记--RadioGroup RadioButton
- Android基础——RadioGroup