1.先写一个流式的class,代码如下:
public class MyFlow extends LinearLayout {
private int mwidth;//得到手机的宽度
private Context mContext;
public MyFlow(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
DisplayMetrics met = context.getResources().getDisplayMetrics();//得到手机的屏幕
mwidth = met.widthPixels;//得到手机的宽度
setOrientation(VERTICAL);//设置手机父级linearlayout的方向
}
public void setData(List<String> list) {
LinearLayout linear = getLinear();
for (int i = 0; i < list.size(); i++) {
String list_item = list.get(i);
int width = 0; //定义一个宽度,相当于手机屏幕的宽度,因为要做比较
int childCount = linear.getChildCount();//控件数量
for (int j = 0; j < childCount; j++) {
TextView tv = (TextView) linear.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
int leftMargin = layoutParams.leftMargin;
tv.measure(getMeasuredWidth(), getMeasuredHeight());//进行测量
width += tv.getMeasuredWidth() + leftMargin + tv.getPaddingLeft() + tv.getPaddingRight();//计算子控件的详细大小
}
TextView datatext = getText();
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = 8;
layoutParams.topMargin = 3;
datatext.setLayoutParams(layoutParams);
datatext.setText(list_item);
datatext.measure(getMeasuredWidth(), getMeasuredHeight());
int dataTextWidth = datatext.getMeasuredWidth() + datatext.getPaddingLeft() + datatext.getPaddingRight();
if (mwidth>=dataTextWidth + width) {//判断如果文字长度+控件长度小于屏幕
linear.addView(datatext);
// list.clear();
} else {
linear = getLinear();
linear.addView(datatext);
}
}
}
private LinearLayout getLinear() {//这个方法是为了创建子linearLayout的子容器,默认垂直显示
LinearLayout linear = new LinearLayout(mContext);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(layoutParams);
//linear.setOrientation(HORIZONTAL);
this.addView(linear);//这里的this是用来表示父容器
return linear;
}
private TextView getText() {
TextView textview = new TextView(mContext);
textview.setTextSize(20);
textview.setTextColor(Color.RED);
textview.setBackgroundResource(R.drawable.ignb);
textview.setPadding(10, 10, 10, 10);
return textview;
}
public void remove(){
removeAllViews();
}
}
2.在写一个组合式控件实现搜索历史记录
public class MyHeader extends RelativeLayout {
private EditText edit_text;
private Button btn_add;
public MyHeader(Context context, AttributeSet attrs) {
super(context, attrs);
View v = View.inflate(context, R.layout.layout_header, this);
edit_text=v.findViewById(R.id.edit_text);
btn_add=v.findViewById(R.id.btn_add);
}
public void setEditText(String mText){
edit_text.setText(mText);
}
public Button getButton(){
return btn_add;
}
public EditText getEditText() {
return edit_text;
}
}
3.xml文件里引入两个自定义控件:注意:头部的高需要写定高度,不然默认充满全屏
<?xml version="1.0" encoding="utf-8"?> <com.example.MyHeader
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/header"
></com.example.lx.rikao3.MyHeader>
<com.example.MyFlow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ignb"
></com.example.lx.rikao3.MyFlow>
4.最后在mainactivity里添加历史记录
public class MainActivity extends AppCompatActivity {
private MyHeader header;
private MyFlow ignb;
List<String> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
header.getButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = header.getEditText();
if(editText.getText().toString().isEmpty()){
return;
}
String string = editText.getText().toString().trim();
ignb.remove();
list.add(string);
ignb.setData(list);
}
});
}
private void initView() {
header = findViewById(R.id.header);
ignb = findViewById(R.id.ignb);
}
}