首先创建两个类,一个类是头部的搜索页面,一个是下面历史搜索和热门搜索的页面
第一头部类
package com.example.yi.myview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.yi.R;
public class MyHead extends LinearLayout {
private EditText sousuo;
private TextView add;
public MyHead(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.head, this);
sousuo=findViewById(R.id.sousuo);
add=findViewById(R.id.add);
}
public String getText(){
return sousuo.getText().toString();
}
public TextView getAdd(){
return add;
}
}
第二历史搜索和热门搜索的流失布局
package com.example.yi.myview;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MyFooter extends LinearLayout {
private final int mScreenWidth;
private final int mScreenHeight;
private String mColor;
public MyFooter(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mScreenWidth = metrics.widthPixels;
mScreenHeight = metrics.heightPixels;
setOrientation(VERTICAL);
/* TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupDemoView);
if (typedArray!=null){
mColor = (String) typedArray.getText(R.styleable.GroupDemoView_textColor);
}*/
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
public void setData(ArrayList<String> data){
LinearLayout linearLayout=getLin();
for (int i = 0; i < data.size(); i++) {
String shuju=data.get(i);
int numWidth=0;
int childCount = linearLayout.getChildCount();
for (int j = 0; j <childCount ; j++) {
TextView tv = (TextView) linearLayout.getChildAt(j);
//LayoutParams params = (LayoutParams) tv.getLayoutParams();
tv.measure(tv.getMeasuredWidth(),tv.getMeasuredHeight());
numWidth+=tv.getMeasuredWidth()+tv.getPaddingLeft()+tv.getPaddingRight();
}
TextView dataText=getText();
LayoutParams layoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
dataText.setLayoutParams(layoutParams);
dataText.setText(shuju);
dataText.measure(getMeasuredWidth(),getMeasuredHeight());
int i1 = dataText.getMeasuredWidth() + dataText.getPaddingRight() + dataText.getPaddingLeft();
if (mScreenWidth>=numWidth+i1){
linearLayout.addView(dataText);
}else{
linearLayout=getLin();
linearLayout.addView(dataText);
}
}
}
public void removeChildView() {
//移除所有子控件
removeAllViews();
}
private TextView getText() {
TextView textView=new TextView(getContext());
textView.setTextSize(20);
textView.setTextColor(Color.RED);
textView.setPadding(10,10,10,10);
return textView;
}
public LinearLayout getLin(){
LinearLayout linearLayout=new LinearLayout(getContext());
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
this.addView(linearLayout);
return linearLayout;
}
}
接下来在activity里用mian布局的控件ID调用两个类的方法
package com.example.yi;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.yi.myview.MyFooter;
import com.example.yi.myview.MyHead;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private MyHead head;
private TextView shanchu;
private MyFooter lishishuju;
private MyFooter remen;
private String[] data = {"流感", "咳嗽", "过敏", "发烧", "感冒", "湿疹", "便秘", "痔疮", "协和", "鼻炎", "失眠", "痛风", "上火", "脚气", "抑郁症", "性欲", "乳腺增生", "头晕", "腰痛"};
private ArrayList<String> mList = new ArrayList<>();
private ArrayList<String> mHistory = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initData() {
for (int i = 0; i <data.length ; i++) {
mList.add(data[i]);
}
}
private void initView() {
head = (MyHead) findViewById(R.id.head);
head.getAdd().setOnClickListener(this);
shanchu = (TextView) findViewById(R.id.shanchu);
shanchu.setOnClickListener(this);
remen = (MyFooter) findViewById(R.id.remen);
remen.setData(mList);
lishishuju = (MyFooter) findViewById(R.id.lishishuju);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.shanchu:
mHistory.clear();
lishishuju.removeChildView();
break;
case R.id.add:
String trim = head.getText().trim();
lishishuju.removeChildView();
mHistory.add(trim);
lishishuju.setData(mHistory);
break;
}
}
}
好看不好看就在布局了,接下来大家看看
先看main的,mian里调用了两个自定义类,下面的历史布局和热门布局都是一个类都用了流失布局的写法
<?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">
<com.example.yi.myview.MyHead
android:id="@+id/head"
android:layout_width="match_parent"
android:layout_height="80dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:paddingLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索历史"/>
<TextView
android:id="@+id/shanchu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:text="删除"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<com.example.yi.myview.MyFooter
android:id="@+id/lishishuju"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="热门搜索" />
<com.example.yi.myview.MyFooter
android:id="@+id/remen"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在看头部的布局
<?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">
<EditText
android:id="@+id/sousuo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="5dp"
android:hint="搜索" />
<TextView
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="添加" />
</LinearLayout>
就这么多了