流式布局

attrs:

declare-styleable name="GroupDemoView"
        attr name="textColor" format="string" /
    /declare-styleable

默认颜色:
app:textColor="@color/colorPrimaryDark"
shape:

shape xmlns:android="http://schemas.android.com/apk/res/android"
    stroke android:width="1dp" android:color="#F3F3F3"/
    corners android:radius="15dp"/
/shape

流式布局:

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<String> data) {
    LinearLayout linearLayout = getLin();//[lvxx,lxs,lzs,lzzs]
    for (int i = 0; i < data.size(); i++) {//lvxx
        final String tmp = data.get(i);
        int numWidth = 0;
        //得到一行LinearLayout到底有多少子控件  因为我要计算每个子控件加在一起的宽度
        int childCount = linearLayout.getChildCount();
        //这个for循环只是计算一行LinearLayout的所有子控件的宽的和
        for (int j = 0; j < childCount; j++) {
            //通过index得到每一个子控件
            TextView tv = (TextView) linearLayout.getChildAt(j);
            LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
            int leftMargin = layoutParams.leftMargin;
            //测量这个tv的高和宽
            tv.measure(getMeasuredWidth(), getMeasuredHeight());
            numWidth += tv.getMeasuredWidth() + leftMargin + tv.getPaddingLeft() + getPaddingRight();
        }

        TextView dataText = getText();
        dataText.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 = 10;
        dataText.setLayoutParams(params);
        dataText.setText(tmp);
        dataText.measure(getMeasuredWidth(), getMeasuredHeight());
        int dataTextWidth = dataText.getMeasuredWidth() + dataText.getPaddingLeft() + dataText.getPaddingRight();
        //考虑到一个字符串很长 就直接超过整个屏幕的高了
//            if (dataTextWidth>=mScreenWidth){
//                String s = tmp.substring(0, 4);
//                dataText.setText(s+"...");
//                dataText.measure(getMeasuredWidth(), getMeasuredHeight());
//                dataTextWidth = dataText.getMeasuredWidth();
//            }

        if (mScreenWidth >= numWidth + dataTextWidth) {//lvxx
            linearLayout.addView(dataText);
        } else {
            //这里面对LinearLayout重新赋值  通过getLin换行
            linearLayout = getLin();
            linearLayout.addView(dataText);
        }
    }
}
//初始化子LinearLayout

private LinearLayout getLin() {
    LinearLayout linearLayout = new LinearLayout(getContext());
    //LayoutParams 控制组件大小的一个工具类
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(params);
    //this本类对象
    this.addView(linearLayout);//只要重新添加View了自动换行了
    return linearLayout;
}
//初始化TextView
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, 3, 10, 3);
    return textView;
}

}
Main:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String[] data = {"流感", "咳嗽", "过敏", "发烧", "感冒", "湿疹", "便秘", "痔疮", "协和", "鼻炎", "失眠", "痛风", "上火", "脚气", "抑郁症", "性欲", "乳腺增生", "头晕", "腰痛"};
    private MyFloatLayout MyFloat_Layout;
    private MyFloatLayout mHistoryLayout;
    private MyXHView mHeadView;
    private MyDao mDao;
    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<String> mHistory = new ArrayList<>();
    private TextView mDelete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDao = new MyDao(this);
    mHistory = mDao.selectName();
    initData();
    initViews();
    if (!mHistory.isEmpty()) {
        mHistoryLayout.setData(mHistory);
    }
}


private void initData() {
    for (int i = 0; i < data.length; i++) {
        mList.add(data[i]);
    }
}

private void initViews() {
    mDelete = findViewById(R.id.Delete_Text);
    mDelete.setOnClickListener(this);
    MyFloat_Layout = findViewById(R.id.MyFloat_Layout);
    MyFloat_Layout.setData(mList);
    mHistoryLayout = findViewById(R.id.MyFloat_Layout_History);
    mHeadView = findViewById(R.id.header_View);
    mHeadView.getmCancel().setOnClickListener(this);
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.Cancel_Text:
            String name = mHeadView.getEditStr().trim();
            mDao.insertSqlite(mHeadView.getEditStr().trim());
            //自己封装了一个方法删除子控件
            mHistoryLayout.removeChildView();
            mHistory.add(name);
            mHistoryLayout.setData(mHistory);
            break;
        case R.id.Delete_Text:
            mDao.delete();
            mHistoryLayout.removeChildView();
            break;
    }
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值