流式布局的其中两个页面

**

  • 获取两种屏幕宽和高的方式

  • 最常用的是 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值