一
package com.example.liuhuan20181025; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; /** * Created by 。。。。 on 2018/10/25. */ public class FlowLayout extends ViewGroup { public FlowLayout(Context context) { this(context, null); } 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 onLayout(boolean changed, int l, int t, int r, int b) { measureChildren(0, 0); int totalWidth = 0; int totalHeight = 0; for (int i = 0; i < getChildCount(); i++) { View view = getChildAt(i); if (totalWidth + view.getMeasuredWidth() >= getMeasuredWidth()) { totalWidth = 0; totalHeight += view.getMeasuredHeight(); } view.layout( totalWidth, totalHeight, totalWidth + view.getMeasuredWidth(), totalHeight + view.getMeasuredHeight()); totalWidth+=view.getMeasuredWidth(); } } }
二
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et_text" android:layout_width="0dp" android:layout_weight="5" android:background="@drawable/radio" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:text="搜索"/> <Button android:id="@+id/btn_decrease" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:text="清除"/> </LinearLayout> <com.example.liuhuan20181025.FlowLayout android:id="@+id/flow" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.example.liuhuan20181025.FlowLayout> <!--<TextView--> <!--android:id="@+id/home_text"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="match_parent" />--> <!--<com.recker.flybanner.FlyBanner--> <!--android:id="@+id/images"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content"></com.recker.flybanner.FlyBanner>--> </LinearLayout>
三
package com.example.liuhuan20181025; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.SearchView; import android.widget.TextView; import com.recker.flybanner.FlyBanner; import java.util.ArrayList; import java.util.List; public class HomeActivity extends AppCompatActivity { private FlyBanner banner; private List<String> list; private EditText searchView; private Button btnadd; private Button btndecrease; private FlowLayout fl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); searchView = findViewById(R.id.et_text); fl = findViewById(R.id.flow); btnadd = findViewById(R.id.btn_add); btndecrease = findViewById(R.id.btn_decrease); addHistor(); btnadd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addHistor(); } }); btndecrease.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fl.removeAllViews(); } }); // banner = findViewById(R.id.images); // list = new ArrayList<>(); // list.add("http://www.zhaoapi.cn/images/quarter/ad1.png"); // list.add("http://www.zhaoapi.cn/images/quarter/ad2.png"); // list.add("http://www.zhaoapi.cn/images/quarter/ad3.png"); // list.add("http://www.zhaoapi.cn/images/quarter/ad4.png"); // banner.setImagesUrl(list); } private void addHistor() { TextView txt=new TextView(this); String s=searchView.getText().toString(); if(!TextUtils.isEmpty(s)){ txt.setText(""+s); txt.setPadding(15,15,15,15); fl.addView(txt); } } }