流式布局的使用

来源:互联网 发布:oracle认证java程序员 编辑:程序博客网 时间:2024/06/14 04:33

流式布局

 ScrollView scrollView = new ScrollView(UIUtils.getContext());    //创建一个流式布局    FlowLayout flowLayout = new FlowLayout(UIUtils.getContext());    for (int i = 0;i < mData.size() ; i++){        TextView textView = new TextView(UIUtils.getContext());        final String text = mData.get(i);        textView.setClickable(true);        textView.setText(text);        //设置背景选择器  未选中的        GradientDrawable nomeDrawable = new GradientDrawable();        //设置圆角        nomeDrawable.setCornerRadius(6);        //随机数        Random random = new Random();        int alpha = 255;        int red = random.nextInt(190) + 30;//不应该等于255        int green = random.nextInt(190) + 30;        int blue = random.nextInt(190) + 30;        int argb = Color.argb(alpha, red, green, blue);        nomeDrawable.setColor(argb);        //设置选中        GradientDrawable pressDrawable = new GradientDrawable();        pressDrawable.setCornerRadius(6);        pressDrawable.setColor(Color.BLUE);        //创建选择器        StateListDrawable ste = new StateListDrawable();        ste.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);        ste.addState(new int[]{}, nomeDrawable);        textView.setBackgroundDrawable(ste);        textView.setTextColor(Color.WHITE);        textView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(UIUtils.getContext(),text,Toast.LENGTH_SHORT).show();            }        });        //设置控件内部的内容居中        textView.setGravity(Gravity.CENTER);        flowLayout.addView(textView);    }    scrollView.addView(flowLayout);    return scrollView;}
原创粉丝点击