自定义属性
<resources>
<declare-styleable name="flow">
<attr name="text_size" format="integer" />
<attr name="text_color" format="integer" />
</declare-styleable>
FlowView
public class FlowView extends FrameLayout {
int txt_size;
int txt_color;
public FlowView(Context context) {
super(context);
}
public FlowView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
txt_color = array.getInt(R.styleable.flow_text_color, 0xFF0000FF);
txt_size = array.getInt(R.styleable.flow_text_size, 0);
}
// public void getData(final String data) {
// TextView textView = (TextView) View.inflate(getContext(), R.layout.flow_item, null);
// textView.setText(data);
// textView.setTextColor(txt_color);
// textView.setTextSize(txt_size);
// FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup
// .LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
// textView.setLayoutParams(layoutParams);
// addView(textView);
// textView.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(getContext(), data, Toast.LENGTH_SHORT).show();
// }
// });
// }
//测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//布局
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//获得控件宽度
int width = getWidth();
//定义常量行数
int row = 0;
//子控件左边的坐标
int disWidth = 18;
for (int i = 0; i < getChildCount(); i++) {
//子控件视图
View view = getChildAt(i);
//子控件宽高
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
//判断子控件宽度+距左侧距离大于控件宽度
if (disWidth + viewWidth > width) {
row++;//换行
disWidth = 18;//距离左侧距离
}
view.layout(disWidth, row * viewHeight, viewWidth + disWidth, viewHeight * (row + 1));
disWidth += viewWidth;
}
}
//绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
Layout
<android.support.v7.widget.SearchView
android:id="@+id/seach"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape"
app:queryHint="请输入要搜索的商品..."></android.support.v7.widget.SearchView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:text="热搜"
android:textSize="20dp" />
<com.songyang.com.day_0104.FlowView
android:id="@+id/flowview1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="Apple"
android:textColor="#f00"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="小米"
android:textColor="#f00"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="OPPO"
android:textColor="#f00"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="ViVO"
android:textColor="#f00"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10sp"
android:text="笔记本"
android:textColor="#f00"
android:textSize="22sp" />
</com.songyang.com.day_0104.FlowView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:text="历史搜索"
android:textSize="20dp" />
<com.songyang.com.day_0104.FlowView
android:id="@+id/flowview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"></com.songyang.com.day_0104.FlowView>
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="清空历史记录"
android:textSize="25dp" />
Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
list = new ArrayList<>();
params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams
.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
seach.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
list.add(s);
textView = new TextView(MainActivity.this);
// textView.setText(list.get(list.size() - 1));
textView.setText(s);
textView.setTextSize(25);
// textView.setTextColor(Color.RED);
textView.setPadding(25, 15, 25, 15);
flowview.addView(textView, params);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, textView.getText().toString(), Toast
.LENGTH_SHORT).show();
textView.setClickable(true);
}
});
// flowview.getData(s);
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return true;
}
});
}
@OnClick(R.id.btn_clear)
public void onViewClicked() {
flowview.removeAllViews();
}