Android RadioGroup 横向显示(两行两列)
来源:互联网 发布:算法导论 高清pdf 编辑:程序博客网 时间:2024/06/03 21:31
做的android平板项目中,需要做一个调查问卷的界面,所以需要使用radiogroup的单选按钮,在listview的一个item上添加四个radiobutton,具体的demo是参考这位博主的,链接如下:http://blog.csdn.net/who0am0i/article/details/45200709,里面解决了我最大的难题就是如何在一个item中,识别用户点击的是哪一个radio,运行结果如下:
可以看到上方的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 横向显示
- Android RadioGroup,显示问题
- Android RadioGroup中横向、竖向布局RadioButton的问题
- Android RadioGroup中横向、竖向布局RadioButton的问题
- 【android】RadioGroup+RadioButton+HorizontalScrollview实现顶部横向菜单栏
- Android horizontal+listview 横向显示
- 自定义横向的RadioGroup:HorizontalRadioGroup
- Android GridView 横向滚动 一行显示
- android 屏幕显示设定为横向
- Android GridView 横向滚动 一行显示
- adobe air for android 固定横向显示
- Android GridView 横向滚动 一行显示
- Android GridView 横向滚动 一行显示
- Android GridView 横向滚动 一行显示
- Android RadioGroup
- Android RadioGroup多行显示,解决单选问题
- Android入门之RadioGroup(纯java)
- 动态SQL语句
- cassandra学习错误问题整合
- Java中的四种权限
- 一,nginx与tomcat整合实现负载均衡
- C#获取Excel全部的sheet
- Android RadioGroup 横向显示(两行两列)
- xshell 自动登录与自动跳转
- java 读取文件
- 【hdu 2147】kiki's game
- appRTC在Ubuntu14.04环境下的服务器搭建以及ios客户端代码编译,android客户端下载
- Android 内存优化-实例讲解MAT
- 2015 build ogre
- 关于Jetty同个服务开启双端口
- Android项目目录与sdk目录下文件的含义