自定义流式布局
package com.example.streamingdemo.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class LiuShiView extends ViewGroup {
private int measuredWidth;
private int size;
public LiuShiView(Context context) {
this(context,null);
}
public LiuShiView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public LiuShiView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//子控件距离父控件的左边距
int width=0;
//子控件距离父控件的高
int height=0;
//子空间之间的左边距
int marginLeft=50;
//子空间的数量
for (int i=0;i<getChildCount();i++){
//获取子控件的下标
View childAt = getChildAt(i);
//获取子控件距离父控件的高
int measuredHeight = childAt.getMeasuredHeight();
//获取子控件距离父控件的宽
measuredWidth = childAt.getMeasuredWidth();
//判断子控件的长度如果大于父控件的长度就换行
if (width+marginLeft+measuredHeight>size){
//换行以后的高度
height=height+measuredHeight;
//换行以后的高度归0
}
//子控件 左边距 右边距 高
childAt.layout(width+marginLeft,height,width+measuredWidth+marginLeft,height+measuredHeight);
//输入第二个以后的控件的高度
width=width+measuredWidth+measuredHeight;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取子控件的高度
measureChildren(widthMeasureSpec,heightMeasureSpec);
//将父控件的宽度设为全局
size=MeasureSpec.getSize(widthMeasureSpec);
}
}
主MainActivity调用
package com.example.streamingdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.streamingdemo.view.LiuShiView;
public class MainActivity extends AppCompatActivity {
private EditText viewShu;
private Button viewShou;
private LiuShiView history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void onclick(View view) {
//将输入框转换为字符串
String s = viewShu.getText().toString();
TextView textView = new TextView(this);
textView.setText(s);
//添加到搜索历史
history.addView(textView);
}
private void initView() {
viewShu = (EditText) findViewById(R.id.view_shu);
viewShou = (Button) findViewById(R.id.view_shou);
history = (LiuShiView) findViewById(R.id.history);
}
}
主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/view_shu"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="请输入内容"/>
<Button
android:id="@+id/view_shou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="onclick"
android:text="搜索"/>
</RelativeLayout>
<!--父控件的搜索框-->
<com.example.streamingdemo.view.LiuShiView
android:id="@+id/history"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--<com.example.zdviews.UserView
android:id="@+id/history"
android:layout_width="match_parent"
android:layout_height="match_parent"/>-->
</LinearLayout>