RadioGroup的换行、换行后的均分实现

问题:Radiogroup在横排现实的时候可能超出屏幕宽度

方法1:为Radiogroup添加 ScrollView()

方法2:使Radiogroup换行;

方法一很简单这里就不说了,主要说一下方法2:

考虑到要为Radiogroup要换行所以要使用到布局方法:onLayout();

和测量方法:onMeasure();当然还要继承Radiogroup的所有属性。

1.在从写onMeasure方法时要注意有:

a、Radiogroup的子个数

b、子View的宽和高;

c、判断子view是否显示;

2.onLayout在这里主要是确定是否要换行注意逻辑就行了。

不多说直接看代码:

/**
 * Created by Hear on 2016/5/6.
 */
public class MyRadioGroup extends RadioGroup{
    public MyRadioGroup(Context context) {
        this(context, null);
    }

    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 复写测量的方法
     *
     * @param widthMeasureSpec  宽度
     * @param heightMeasureSpec 高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取到孩子的个数
        int childCount = getChildCount();
        int x = 0;
        int y = 0;
        int row = 0;
        //获取到最大宽度
        int maxWidth = View.MeasureSpec.getSize(widthMeasureSpec);


        //遍历孩子,并对它们进行测量
        for (int i = 0; i < childCount; i++) {
            //获取到孩子
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {

                child.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight(); //计算出大小
                y = height * (row + 1);
                x += width;

                if (x > maxWidth) {
                    row++;
                    //如果大于了最大的宽度,那么就换行咯
                    x = width;
                    y = height * (row + 1) + height / 2;
                }
            }
            setMeasuredDimension(maxWidth, y);
            Log.d("message", "y = " + y);
        }
    }

    /**
     * 复写布局方法
     *
     * @param changed 是否改变
     * @param l       左
     * @param t       上
     * @param r       右
     * @param b       下
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //获取到孩子的个数
        int childCount = getChildCount();
        int x = 0;
        int y = 0;
        int row = 0;
        //间隙view之间
        int weight = 50;

        //计算最大宽度
        int maxWidth = r - l;

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                //计算XY
                x += width+width/3;
                if (row!=0){
                    y = (row + 1) * height+height/2;
                }else {
                    y = (row + 1) * height;
                }
                if (x > maxWidth) {
                    //换行
                    row++;
                    x = width+width/3;
                    y = (row + 1) * height+height/2;
                }
                //设置孩子的位置
                child.layout(x - width, y - height, x, y);
            }
        }

    }
}


ScrollView


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,RadioGroup是一个Android视图组件,它通常用于提供一组单选按钮。它可以让用户从几个选项中选择一个选项。 要实现页面跳转,您需要在RadioGroup中添加单选按钮,然后将每个单选按钮与相应的Activity关联。 以下是一个简单的示例,它使用RadioGroup和RadioButton来实现页面跳转: 1.在您的layout文件中添加RadioGroup和RadioButton组件。例如: ``` <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page 1"/> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page 2"/> </RadioGroup> ``` 2.在您的Activity中获取RadioGroup的引用,然后为每个RadioButton添加点击事件处理程序。在每个处理程序中,使用Intent对象将用户重定向到所选Activity。例如: ``` RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { Intent intent; switch (checkedId) { case R.id.radioButton1: intent = new Intent(MainActivity.this, Page1Activity.class); startActivity(intent); break; case R.id.radioButton2: intent = new Intent(MainActivity.this, Page2Activity.class); startActivity(intent); break; default: break; } } }); ``` 在上面的代码中,我们为RadioGroup设置了一个监听器,当用户单击单选按钮时,onCheckedChanged()方法将被调用。在这个方法中,我们使用switch语句将用户重定向到所选Activity。 这就是使用RadioGroup实现页面跳转的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值