**
-
获取两种屏幕宽和高的方式
-
最常用的是 DisplayMetrics
-
WindowManager 不常用了 我查看已经加上删除线 过时
*/
public class MyFloatLayout extends LinearLayout {
private int mScreenWidth;
private int mScreenHeight;
private String mColor;public MyFloatLayout(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);
}
}
public void removeChildview() {
removeAllViews();
}@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 data) {
LinearLayout linearLayout = getLin();
for (int i = 0; i < data.size(); i++) {
final String tmp = data.get(i);
int num = 0;
int childerCount = linearLayout.getChildCount();
for (int j = 0; j < childerCount; j++) {
TextView textView = (TextView) linearLayout.getChildAt(j);
LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
int leftMargin = layoutParams.leftMargin;textView.measure(getMeasuredWidth(), getMeasuredHeight()); num += textView.getMeasuredWidth() + leftMargin + textView.getPaddingLeft() + getPaddingRight(); } TextView text1 = getText(); text1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), tmp, Toast.LENGTH_SHORT).show(); } }); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.leftMargin = 15; params.topMargin = 15; text1.setLayoutParams(params); text1.setText(tmp); text1.measure(getMeasuredWidth(), getMeasuredHeight()); int datatextWidth = text1.getMeasuredWidth(); if (metricsWidth >= num + datatextWidth) { linearLayout.addView(text1); } else { linearLayout = getLin(); linearLayout.addView(text1); } }
}
private TextView getText() {
TextView textView = new TextView(getContext());
textView.setTextSize(20);
textView.setTextColor(Color.parseColor(mcolor));
textView.setBackgroundResource(R.drawable.text_style);
textView.setPadding(10, 10, 10, 10);
return textView;}
private LinearLayout getLin() {
LinearLayout linearLayout = new LinearLayout(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
this.addView(linearLayout);
return linearLayout;}
public class MyXHView extends LinearLayout {
private EditText mEditText;
private TextView mCancel;
public MyXHView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.header_view, this);
mEditText = findViewById(R.id.Search_Edit);
mCancel = findViewById(R.id.Cancel_Text);
}
public String getEditStr() {
return mEditText.getText().toString();
}
public TextView getmCancel() {
return mCancel;
}