MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtMain; private Button mBtnMain; private FlowLayout mFlMain;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
}
private void initView() {
mEtMain = (EditText) findViewById(R.id.et_main);
mBtnMain = (Button) findViewById(R.id.btn_main);
mFlMain = (FlowLayout) findViewById(R.id.fl_main);
mBtnMain.setOnClickListener(this);
}
@Override public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_main: submit(); break;
}
}
private void submit() {
// validate
String main = mEtMain.getText().toString().trim();
if (TextUtils.isEmpty(main)) { Toast.makeText(this, "main不能为空",
Toast.LENGTH_SHORT).show(); return;
} else {
TextView textView = new TextView(getApplicationContext());
textView.setText(main);
textView.setTextColor(Color.BLACK);
textView.setPadding(10, 10, 10, 10);
mFlMain.addView(textView); } }}
FlowLayout
public class FlowLayout extends ViewGroup {
private static final String TAG = "FlowLayout++";
public FlowLayout(Context context) {
super(context); }
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FlowLayout(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 widthSize = MeasureSpec.getSize(widthMeasureSpec);
//AT-MOST
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize); }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
int lineWidth = 0;
//int lineHeight = 0;
int totalHeight = 0;
//累加的高度 View childView;
int childWidth = 0;
int childHeight = 0;
for (int i = 0; i < getChildCount(); i++)
{
childView = getChildAt(i);
childWidth = childView.getMeasuredWidth();
childHeight = childView.getMeasuredHeight();
//测量布局的宽 //换行/不换行
if (childWidth + lineWidth > getMeasuredWidth()) {
//换行
totalHeight += childHeight;
lineWidth = 0;
childViewLayout(childView, lineWidth, totalHeight, lineWidth + childWidth, totalHeight + childHeight);
//第一个
lineWidth = childWidth; }
else {
//不换行
childViewLayout(childView, lineWidth, totalHeight, lineWidth + childWidth, totalHeight + childHeight);
lineWidth += childWidth;
} } }
public void childViewLayout(View childView, int l, int t, int r, int b) { childView.layout(l, t, r, b); }}
<?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:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et_main" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/btn_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" /> </LinearLayout> <com.example.flowlayout.FlowLayout android:id="@+id/fl_main" android:layout_width="match_parent" android:layout_height="200dp"> </com.example.flowlayout.FlowLayout> </LinearLayout>