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>
效果图
0 0
- Android流式布局的简单实现
- Android流式布局的简单实现
- android 流式布局简单实现
- Android 简单实现流式布局
- Android流式布局实现
- Android流式布局实现
- Android流式布局实现
- Android流式布局实现
- 实现Android流式布局
- Android开发学习之卡片式布局的简单实现
- Android开发学习之卡片式布局的简单实现
- Android卡片式布局的简单实现方法
- 简单实现响应式布局
- Android之路-简单布局的实现
- Android RecyclerView加载不同布局简单实现
- Android 简单 相对布局的实现;
- Android 流式布局实现方法
- Android 流式布局实现方法
- Paxos开源实现
- Android 加载其他Apk中的资源文件
- Android 透明状态栏
- CocoaPod 版本升级
- Android轻松实现ListView下拉放大
- Android 简单实现流式布局
- AndroidStudio 用正式签名调试应用
- Android不规则形状点击检测
- 3d拖拽相册
- cisco linux english word
- 导出/导入DataPump参数TRACE - 如何诊断Oracle数据泵(文档 ID 286496.1)
- 在CentOS下搭建自己的Git服务器
- MySQL 大小写敏感修改不了
- Qt5.5 QFileDialog类的使用方法