Android RadioGroup 横向显示(两行两列)

       做的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);
	}

}

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值