首页 > 解决方案 > 在 Android 中生成动态视图

问题描述

我想生成如下视图。

动态视图布局

这个视图应该是动态生成的。这意味着视图总数可能会不时变化。最多可以有 6 到 7 个视图。我尝试使用线性布局来实现这一点,但是当设备宽度发生变化时,视图会像这样发生变化。

在此处输入图像描述

这是我的代码。

List<String> ticketNumbers = Arrays.asList(buyLottery.getTicketNumbers());
    LinearLayout[] linearLayoutArr = null;

    linearLayoutArr = new LinearLayout[ticketNumbers.size()];
    holder.llLotteryViewContainer.removeAllViews();

    // Setting dimensions of Views
    int width = Values.pxToDp(context, 45);
    int height = Values.pxToDp(context, 45);
    int textSize = Values.pxToSp(context, 6);
    int margin = Values.pxToDp(context, 5);
    int imgWidth = Values.pxToDp(context, 45);
    int imgHeight = Values.pxToDp(context, 45);

    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
    params2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    params2.gravity = Gravity.CENTER;
    params2.weight = 1.0f;

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    params.setMargins(margin, margin, margin, margin);

    for (int i = 0; i < linearLayoutArr.length; i++) {

        linearLayoutArr[i] = new LinearLayout(context);

        if (linearLayoutArr[i] != null) {

            linearLayoutArr[i].setOrientation(LinearLayout.HORIZONTAL);
            linearLayoutArr[i].setLayoutParams(params2);

            // <editor-fold default state="collapsed" desc="Digit">
            if (Character.isDigit(ticketNumbers.get(i).charAt(0))) {

                textView = null;
                textView = new TextView(context);
                textView.setWidth(width);
                textView.setHeight(height);
                textView.setText(ticketNumbers.get(i));
                textView.setTextSize(textSize);
                textView.setGravity(Gravity.CENTER);
                textView.setTextColor(context.getResources().getColor(R.color.black));
                textView.setLayoutParams(params);

                if (selectedLottery.getBonusNumber().equals("YES") && Integer.parseInt(selectedLottery.getBonusNumberPos()) - 1 == i) {
                    textView.setBackground(ContextCompat.getDrawable(textView.getContext(), R.drawable.background_bonus_bubble));
                    linearLayoutArr[i].addView(textView);
                    holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);
                    holder.llLotteryViewContainer.invalidate();
                } else {
                    textView.setBackground(context.getResources().getDrawable(R.drawable.ic_background_normal_bubble));
                    linearLayoutArr[i].addView(textView);
                    holder.llLotteryViewContainer.addView(linearLayoutArr[i]);
                }

            }
            // </editor-fold>
            // <editor-fold default state="collapsed" desc="Letter">
            else if (Character.isLetter(ticketNumbers.get(i).charAt(0))) {

                if (ticketNumbers.get(i).length() > 1) {

                    String zodiac = ticketNumbers.get(i);
                    String zodiacName = Values.getZodiacIdFromName(zodiac);
                    int imgId = Values.getId(zodiacName, R.drawable.class);

                    ImageView imageView = new ImageView(context);
                    imageView.setBackground(context.getResources().getDrawable(imgId));
                    imageView.setColorFilter(context.getResources().getColor(R.color.colorPrimary));
                    imageView.setLayoutParams(params);
                    imageView.getLayoutParams().height = imgHeight;
                    imageView.getLayoutParams().width = imgWidth;
                    linearLayoutArr[i].addView(imageView);
                    holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);

                } else {
                    textView = null;
                    textView = new TextView(context);
                    textView.setWidth(width);
                    textView.setHeight(height);
                    textView.setText(ticketNumbers.get(i));
                    textView.setTypeface(Typeface.DEFAULT_BOLD);
                    textView.setTextSize(textSize);
                    textView.setGravity(Gravity.CENTER);
                    textView.setTextColor(context.getResources().getColor(R.color.black));
                    textView.setLayoutParams(params);
                    linearLayoutArr[i].addView(textView);
                    textView.setBackground(context.getResources().getDrawable(R.drawable.ic_background_special_bubble));
                    holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);
                }

                holder.llLotteryViewContainer.invalidate();
            }
            // </editor-fold>

        }

    }

我想知道构建此布局的更好方法。使用网格布局而不是线性布局更好吗?如果是,我想知道如何。

标签: android

解决方案


推荐阅读