#流失布局 自定义view
public class LiuView extends FrameLayout {
private float mText;
public LiuView(Context context) {
this(context,null);
}
public LiuView(Context context,AttributeSet attrs) {
this(context, attrs,0);
}
public LiuView(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
}
private void init(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LiuView);
typedArray.getDimension(R.styleable.LiuView_textsize,0);
}
public void addTextView(String name){
TextView view =(TextView) View.inflate(getContext(), R.layout.liu_item, null);
view.setText(name);
LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
addView(view);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int pwidth = getWidth();
int row=0;
int disWidth=20;
for (int i=0;i<getChildCount();i++){
View at = getChildAt(i);
int Childwidth = at.getWidth();
int Childheight = at.getHeight();
if (disWidth+Childwidth>pwidth){
row++;
disWidth=20;
}
int i1 = row * Childheight + row * 20;
at.layout(
disWidth,i1,disWidth+Childwidth,i1+Childheight
);
disWidth+=(Childwidth+20);
}
}
}
#布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/edit"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:drawableLeft="@mipmap/ic_launcher"
/>
<Button
android:id="@+id/button"
android:layout_width="80dp"
android:layout_height="80dp"
android:textSize="20dp"
android:text="搜索"/>
</LinearLayout>
<TextView
android:text="历史纪录"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.yuekaomn_20190118.view.LiuView
android:id="@+id/liu_one"
android:textSize="50dp"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="连衣裙"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
</com.example.yuekaomn_20190118.view.LiuView>
<TextView
android:text="热门搜索"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.example.yuekaomn_20190118.view.LiuView
android:textSize="50dp"
android:id="@+id/flow_two"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="11111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="111111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="1111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="1111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="1111141111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="1111141111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="1111141111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="111"
android:background="@drawable/shape_liu"
android:textSize="20sp" />
</com.example.yuekaomn_20190118.view.LiuView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_liu"
android:text="清除历史纪录"/>
</LinearLayout>