<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#babac4" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:background="@drawable/shape_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:layout_width="0dp" android:layout_weight="9" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="请输入想要搜索的书籍" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/a_4" android:id="@+id/loginbtn"/> </LinearLayout> <com.example.asus.week2demo.utils.LiuShi android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/liull"/> </LinearLayout>
MainActivity
package com.example.asus.week2demo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.asus.week2demo.utils.LiuShi;
import com.fynn.fluidlayout.FluidLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.editText)
EditText editText;
@BindView(R.id.loginbtn)
Button loginbtn;
@BindView(R.id.liull)
LiuShi liull;
private List<String> list=new ArrayList<>();
private Unbinder bind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind = ButterKnife.bind(this);
initData();
}
private void initData() {
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String st = editText.getText().toString();
list.add(st);
liull.removeAllViews();
for (int i = 0; i < list.size(); i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(list.get(i));
liull.addView(tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,ShowActivity.class));
}
});
}
liull.setPadding(5, 5, 5, 5);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
bind.unbind();
}
}
LiuShi
package com.example.asus.week2demo.utils; import android.content.Context; import android.util.AttributeSet; import android.view.ViewGroup; public class LiuShi extends ViewGroup { private int mLeftMargin = 20; private int mTopMargin = 20; public LiuShi(Context context) { this(context, null); } public LiuShi(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec); int leftMargin = mLeftMargin; int topMargin = mTopMargin; int ViewWidth = 0; int ViewHeight = 0; int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); int modelWidth = MeasureSpec.getMode(widthMeasureSpec); int modelHeight = MeasureSpec.getMode(heightMeasureSpec); switch (modelHeight) { case MeasureSpec.AT_MOST: int measuredHeight = 0; for (int i = 0; i < getChildCount(); i++) { int measuredWidth = getChildAt(i).getMeasuredWidth(); measuredHeight = getChildAt(i).getMeasuredHeight(); if (leftMargin + measuredWidth + mLeftMargin > getWidth()) { leftMargin = mLeftMargin; topMargin += measuredHeight + mTopMargin; } leftMargin += measuredWidth + mLeftMargin; } topMargin += measuredHeight + mTopMargin; break; } setMeasuredDimension(sizeWidth, topMargin); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int leftMargin = mLeftMargin; int topMargin = mTopMargin; for (int i = 0; i < getChildCount(); i++) { int measuredWidth = getChildAt(i).getMeasuredWidth(); int measuredHeight = getChildAt(i).getMeasuredHeight(); if (leftMargin + measuredWidth + mLeftMargin > getWidth()) { leftMargin = mLeftMargin; topMargin += measuredHeight + mTopMargin; getChildAt(i).layout(leftMargin, topMargin, leftMargin + measuredWidth, topMargin + measuredHeight); } else { getChildAt(i).layout(leftMargin, topMargin, leftMargin + measuredWidth, topMargin + measuredHeight); } leftMargin += measuredWidth + mLeftMargin; } } }