首页 新闻 搜索 专区 学院

Android中RadioGroup换行问题

0
[已解决问题] 解决于 2015-08-21 18:53

这次要实现不同的分组里面有不同数量的RadioGroup不说,还有实现自动换行问题,,,因为有的数量多了,针对小手机的人来说,就导致了这个问题的存在。然后,我尝试在GridView里面用Button和TextView来实现,结果,由于项数多了,导致去判断单选,我觉得太费事了,有咩有遇到过可以实现RadioGroup自动换行的。。。。

Catherine_Brain的主页 Catherine_Brain | 初学一级 | 园豆:162
提问于:2014-12-17 23:52
< >
分享
最佳答案
0

这是一个会自动换行处理的布局layout,应该可以满足你的需要

 

import java.util.Hashtable;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;


public class NewlineLayout extends LinearLayout {
    int mLeft, mRight, mTop, mBottom;
    Hashtable map = new Hashtable();


    public NewlineLayout(Context context) {
        super(context);
    }

    public NewlineLayout(Context context, int horizontalSpacing, int verticalSpacing) {
        super(context);
    }

    public NewlineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int mWidth = MeasureSpec.getSize(widthMeasureSpec);
        int mCount = getChildCount();
        int mX = 0;
        int mY = 0;
        mLeft = 0;
        mRight = 0;
        mTop = 5;
        mBottom = 0;

        int j = 0;

        View lastview = null;
        for (int i = 0; i < mCount; i++) {
            final View child = getChildAt(i);
           
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            // 此处增加onlayout中的换行判断,用于计算所需的高度
            int childw = child.getMeasuredWidth();
            int childh = child.getMeasuredHeight();
            mX += childw;  //将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置

            Position position = new Position();
            mLeft = getPosition(i - j, i);
            mRight = mLeft + child.getMeasuredWidth();
            if (mX >= mWidth) {
                mX = childw;
                mY += childh;
                j = i;
                mLeft = 0;
                mRight = mLeft + child.getMeasuredWidth();
mTop = mY + 5;
//PS:如果发现高度还是有问题就得自己再细调了
            }
            mBottom = mTop + child.getMeasuredHeight();
            mY = mTop;  //每次的高度必须记录 否则控件会叠加到一起
            position.left = mLeft;
            position.top = mTop + 3;
            position.right = mRight;
            position.bottom = mBottom;
            map.put(child, position);
        }
        setMeasuredDimension(mWidth, mBottom);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(1, 1); // default of 1px spacing
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            Position pos = (Position) map.get(child);
            if (pos != null) {
                child.layout(pos.left, pos.top, pos.right, pos.bottom);
            } else {
                Log.i("MyLayout", "error");
            }
        }
    }

    private class Position {
        int left, top, right, bottom;
    }

    public int getPosition(int IndexInRow, int childIndex) {
        if (IndexInRow > 0) {
            return getPosition(IndexInRow - 1, childIndex - 1)
                    + getChildAt(childIndex - 1).getMeasuredWidth() + 8;
        }
        return getPaddingLeft();
    }
}

奖励园豆:5
竹下半碗茶 | 菜鸟二级 |园豆:213 | 2015-08-20 16:36

谢谢

Catherine_Brain | 园豆:162 (初学一级) | 2015-08-21 18:52
其他回答(1)
0

没看明白。

beyondchina | 园豆:680 (小虾三级) | 2015-08-07 21:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册