Android 简单实现流式布局

来源:互联网 发布:怎么发淘宝店铺链接 编辑:程序博客网 时间:2024/04/29 19:08

上代码

public class FlowLayout extends ViewGroup {


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

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

public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
}
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int mViewGroupWidth = getMeasuredWidth(); //容器宽度

int mPainterPosX = 0; //当前绘图X
int mPainterPosY = 0; //当前绘图Y

int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
//宽度大于容器宽度时,则移到下一行开始位置
if (mPainterPosX + width > mViewGroupWidth) {
mPainterPosX = 0;
mPainterPosY += height;
}
//位置摆放
childView.layout(mPainterPosX, mPainterPosY, mPainterPosX + width, mPainterPosY + height);
//记录当前已经绘制到的横坐标位置
mPainterPosX += width;
}

}
}
布局文件里使用 放几个Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="FlowLayout"
android:textColor="@android:color/white"
android:textSize="20sp" />

</LinearLayout>
</android.support.v7.widget.Toolbar>

<com.mz.transition.FlowLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Button" />

<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Button" />

<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Button" />

<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Button" />

<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Button" />


</com.mz.transition.FlowLayout>

</LinearLayout>
效果图
Android 简单实现流式布局 - 有一天 - 有一天的博客
 



0 0
原创粉丝点击