流式布局的shape
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#666666" />
<corners android:radius="10dp" />
<padding
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="5dp"
/>
</shape>
流式布局的主体内容
public class XCFlowLayout extends ViewGroup {
private List<List<View>> mAllChildViews = new ArrayList<>();
private List<Integer> mLineHeight = new ArrayList<>();
public XCFlowLayout(Context context) {
this(context, null);
}
public XCFlowLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public XCFlowLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;
int height = 0;
int lineWidth = 0;
int lineHeight = 0;
int childCount = getChildCount();
for(int i = 0;i < childCount; i ++){
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
if(lineWidth + childWidth > sizeWidth){
width = Math.max(width, lineWidth);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
}else{
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
if(i == childCount -1){
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mAllChildViews.clear();
mLineHeight.clear();
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
List<View> lineViews = new ArrayList<View>();
int childCount = getChildCount();
for(int i = 0;i < childCount; i ++){
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
if(childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width){
mLineHeight.add(lineHeight);
mAllChildViews.add(lineViews);
lineWidth = 0;
lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
lineViews = new ArrayList();
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
lineViews.add(child);
}
mLineHeight.add(lineHeight);
mAllChildViews.add(lineViews);
int left = 0;
int top = 0;
int lineCount = mAllChildViews.size();
for(int i = 0; i < lineCount; i ++){
lineViews = mAllChildViews.get(i);
lineHeight = mLineHeight.get(i);
for(int j = 0; j < lineViews.size(); j ++){
View child = lineViews.get(j);
if(child.getVisibility() == View.GONE){
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int cLeft = left + lp.leftMargin;
int cTop = top + lp.topMargin;
int cRight = cLeft + child.getMeasuredWidth();
int cBottom = cTop + child.getMeasuredHeight();
child.layout(cLeft, cTop, cRight, cBottom);
left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
}
left = 0;
top += lineHeight;
}
}
/**
* 与当前ViewGroup对应的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
}
主MainActivity
public class MainActivity extends AppCompatActivity {
private String mNames[] = {
"应急启动电源", "餐桌", "粽子散装",
"智能手表", "摩托车配件", "批发方便面",
"王中王火腿", "手机", "桶装矿泉水",
"U盘64G", "机械革命电脑", "洗发水",
"护发素", "奶粉", "search", "logcat"
};
private XCFlowLayout mFlowLayout;
private EditText ed_text;
private ListView list_view;
private Button delall;
private List<String> list;
private TextView te_sou;
private MylistAdapter mylistAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<>();
ed_text = findViewById(R.id.ed_text);
list_view = findViewById(R.id.list_view);
delall = findViewById(R.id.delAll);
te_sou = findViewById(R.id.te_sou);
initChildViews();
xiTiao();
ed_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ed_text.setCursorVisible(true);
}
});
delall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.clear();
mylistAdapter.notifyDataSetChanged();
delall.setVisibility(View.GONE);
}
});
}
public void initChildViews() {
mFlowLayout = findViewById(R.id.liu_shi);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.leftMargin = 5;
lp.rightMargin = 5;
lp.topMargin = 5;
lp.bottomMargin = 5;
for(int i = 0; i < mNames.length; i ++){
TextView view = new TextView(this);
view.setText(mNames[i]);
view.setTextColor(Color.WHITE);
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.textview_bg));
mFlowLayout.addView(view,lp);
}
}
public void xiTiao() {
te_sou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shu = ed_text.getText().toString();
list.add(shu);
mylistAdapter = new MylistAdapter(MainActivity.this,list);
list_view.setAdapter(mylistAdapter);
Intent it = new Intent(MainActivity.this,ZhanActivity.class);
startActivity(it);
delall.setVisibility(View.VISIBLE);
}
});
}
}
主MainActivity布局
<RelativeLayout >
<LinearLayout
android:id="@+id/liner"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:src="@drawable/leftjiantou"
android:layout_marginTop="6dp"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_marginTop="8dp"
android:background="@drawable/sousuo_kuang"
android:orientation="horizontal"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0dp"
android:layout_height="40dp">
<ImageView
android:layout_width="25dp"
android:src="@drawable/a_4"
android:layout_margin="7dp"
android:layout_gravity="center_vertical"
android:layout_height="25dp" />
<EditText
android:id="@+id/ed_text"
android:hint="内衣夸店3免1,服装夸店3件7折"
android:layout_gravity="center"
android:textSize="15sp"
android:background="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/root"
android:layout_margin="5dp"
android:layout_width="28dp"
android:layout_height="28dp" />
</LinearLayout>
<TextView
android:id="@+id/te_sou"
android:text="搜索"
android:layout_marginLeft="4dp"
android:layout_marginTop="12dp"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/tere"
android:text="热搜"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="@+id/liner"
android:layout_margin="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.weekone.XCFlowLayout
android:id="@+id/liu_shi"
android:layout_below="@+id/tere"
android:layout_width="match_parent"
android:layout_height="150dp">
</com.example.weekone.XCFlowLayout>
<TextView
android:id="@+id/te_history"
android:visibility="gone"
android:layout_below="@+id/liu_shi"
android:layout_marginTop="10dp"
android:text="历史搜索"
android:textSize="25sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_view"
android:layout_below="@+id/te_history"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:id="@+id/delAll"
android:layout_marginTop="15dp"
android:layout_below="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="清空历史搜索"/>
</RelativeLayout>