您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Android RadioGroup實(shí)現(xiàn)自動(dòng)換行的方法,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
具體內(nèi)容如下:
public class FlowRadioGroup extends RadioGroup {
private static final String TAG = "MyRadioGroup";
public FlowRadioGroup(Context context) {
super(context);
}
public FlowRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//調(diào)用ViewGroup的方法,測量子view
measureChildren(widthMeasureSpec, heightMeasureSpec);
//最大的寬
int maxWidth = 0;
//累計(jì)的高
int totalHeight = 0;
//當(dāng)前這一行的累計(jì)行寬
int lineWidth = 0;
//當(dāng)前這行的最大行高
int maxLineHeight = 0;
//用于記錄換行前的行寬和行高
int oldHeight;
int oldWidth;
int count = getChildCount();
//假設(shè) widthMode和heightMode都是AT_MOST
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
oldHeight = maxLineHeight;
//當(dāng)前最大寬度
oldWidth = maxWidth;
int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
//和目前最大的寬度比較,得到最寬。不能加上當(dāng)前的child的寬,所以用的是oldWidth
maxWidth = Math.max(lineWidth, oldWidth);
//重置寬度
lineWidth = deltaX;
//累加高度
totalHeight += oldHeight;
//重置行高,當(dāng)前這個(gè)View,屬于下一行,因此當(dāng)前最大行高為這個(gè)child的高度加上margin
maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);
} else {
//不換行,累加寬度
lineWidth += deltaX;
//不換行,計(jì)算行最高
int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
maxLineHeight = Math.max(maxLineHeight, deltaY);
}
if (i == count - 1) {
//前面沒有加上下一行的搞,如果是最后一行,還要再疊加上最后一行的最高的值
totalHeight += maxLineHeight;
//計(jì)算最后一行和前面的最寬的一行比較
maxWidth = Math.max(lineWidth, oldWidth);
}
}
//加上當(dāng)前容器的padding值
maxWidth += getPaddingLeft() + getPaddingRight();
totalHeight += getPaddingTop() + getPaddingBottom();
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count = getChildCount();
//pre為前面所有的child的相加后的位置
int preLeft = getPaddingLeft();
int preTop = getPaddingTop();
//記錄每一行的最高值
int maxHeight = 0;
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
//r-l為當(dāng)前容器的寬度。如果子view的累積寬度大于容器寬度,就換行。
if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
//重置
preLeft = getPaddingLeft();
//要選擇child的height最大的作為設(shè)置
preTop = preTop + maxHeight;
maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
} else { //不換行,計(jì)算最大高度
maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
}
//left坐標(biāo)
int left = preLeft + params.leftMargin;
//top坐標(biāo)
int top = preTop + params.topMargin;
int right = left + child.getMeasuredWidth();
int bottom = top + child.getMeasuredHeight();
//為子view布局
child.layout(left, top, right, bottom);
//計(jì)算布局結(jié)束后,preLeft的值
preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
}
}
}
上述就是小編為大家分享的Android RadioGroup實(shí)現(xiàn)自動(dòng)換行的方法了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。