流式布局 Activity
package com.bawei.zhaojianfu2019730.view.activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class Zdyliu extends ViewGroup {
private int size;
public Zdyliu(Context context) {
super(context);
}
public Zdyliu(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left=0;
int top = 0;
int jiange=20;
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
//获取宽高
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
if (left+measuredWidth>=size){
top+=measuredHeight;
left=0;
}
childAt.layout(left,top,left+measuredWidth,top+measuredHeight);
left+=measuredWidth+jiange;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//存入宽高
measureChildren(widthMeasureSpec,heightMeasureSpec);
//获取宽的长度
size = MeasureSpec.getSize(widthMeasureSpec);
}
}
fragement
package com.bawei.zhaojianfu2019730.view.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.bawei.zhaojianfu2019730.R;
import com.bawei.zhaojianfu2019730.view.activity.Zdyliu;
public class fragmentthree extends Fragment {
private EditText fragthree_edit;
private Button fragthree_but;
private Zdyliu fragthree_liu;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragmentthree, null);
fragthree_edit = inflate.findViewById(R.id.fragthree_edit);
fragthree_but = inflate.findViewById(R.id.fragthree_but);
fragthree_liu = inflate.findViewById(R.id.fragthree_liu);
fragthree_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView textView = new TextView(getActivity());
String trim = fragthree_edit.getText().toString().trim();
textView.setText(trim);
ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(
ViewGroup.MarginLayoutParams.WRAP_CONTENT,
ViewGroup.MarginLayoutParams.WRAP_CONTENT
);
fragthree_liu.addView(textView,params);
}
});
return inflate;
}
}
布局
<EditText
android:id="@+id/fragthree_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入搜索内容"
/>
<Button
android:id="@+id/fragthree_but"
android:text="确定"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
本文深入探讨了自定义流式布局Activity的实现方法,通过Zdyliu类的详细介绍,展示了如何在Android应用中实现流式布局,使子视图能够自动换行排列,提供了具体的代码实现和布局文件示例。
2280

被折叠的 条评论
为什么被折叠?



