public class SearchLayout extends LinearLayout {
private final int mParentWidth;
private float textSize;
private boolean textColor;
private boolean background;
private boolean isHide = true;
public void setHide(boolean hide) {
isHide = hide;
}
public SearchLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//获取屏幕的宽度
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mParentWidth = metrics.widthPixels - dip2px(16f);
//自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SearchLayout);
background = array.getBoolean(R.styleable.SearchLayout_Sear_background,false);
textColor = array.getBoolean(R.styleable.SearchLayout_Sear_textColor, false);
textSize = array.getDimension(R.styleable.SearchLayout_Sear_textSize, 0);
//方向为纵向
setOrientation(VERTICAL);
}
//移除子控件
public void removeView() {
removeAllViews();
}
//流式布局
public void setData(List<String> data) {
if (data.isEmpty()){
return;
}
//获取一个子布局
LinearLayout linearLayout = getLinearLayout();
for (int i = 0; i < data.size(); i++) {
//标题
final String name = data.get(i);
//已存在的宽度
int numBar = 0;
//子控件的个数
int count = linearLayout.getChildCount();
for (int j = 0; j < count; j++) {
//一个一个获取
ThemeTextView textView = (ThemeTextView) linearLayout.getChildAt(j);
//获取左外边距
LayoutParams params = (LayoutParams) textView.getLayoutParams();
int leftWidth = params.leftMargin;
int rightWidth = params.rightMargin;
//获取宽高
textView.measure(getMeasuredWidth(), getMeasuredHeight());
//计算已存在的宽度
numBar += textView.getMeasuredWidth()+leftWidth+rightWidth;
}
//获取一个子控件
ThemeTextView text = getText();
//给每一个控件设置点击事件
text.setOnClickListener(new OnClickListener
Android 流式布局(修改版) 当达到两行,隐藏多余的
最新推荐文章于 2022-04-14 08:15:00 发布