gridlayout 中的第一项有什么问题?

what is wrong in this first item in gridlayout?

在这段代码中,我能够创建网格布局。但第一项是默认视图,其余的就像我想要的一样。我尝试调试,但看不出有什么问题。你能帮我解决这个问题吗?


        question="this is a sentence";

        String sLetter;

        String question = question.replace(" ", "");

        //char[] charAr = question.toCharArray();

        final ArrayList<String> letters = new ArrayList<>();
        for (int k = 0; k < question.length(); k++) {
            sLetter = Character.toString(question.charAt(k));
            letters.add(sLetter);
        }

        Collections.sort(letters);
        int i;

        for (i = 0; i < letters.size();) {

            int count = recursiveMethod(letters,i,1);

            if (count>0) {
                //region text add
                View main_view = new View(context);
                main_view.setLayoutParams(new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT));

                gridLayout.setColumnCount(7);
                if (question.length()<=7){
                    gridLayout.setRowCount(1);
                }else if (question.length()<12){
                    gridLayout.setRowCount(2);
                }else {
                    gridLayout.setRowCount(3);
                }
                GridLayout.Spec colSpan = GridLayout.spec(GridLayout.UNDEFINED,1);
                GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
                GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                        rowSpan, colSpan);
                gridParam.setGravity(Gravity.FILL_HORIZONTAL);
                gridParam.setMargins(5, 10, 5, 10);


                final TextView tv = new TextView(context);
                tv.setText(letters.get(i).toUpperCase());
                tv.setId(i);
                tv.setPadding(15, 15, 15, 15);
                tv.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
                RelativeLayout.LayoutParams relTv  = new RelativeLayout.LayoutParams(80, ViewGroup.LayoutParams.WRAP_CONTENT);
                relTv.addRule( RelativeLayout.CENTER_HORIZONTAL);
                relTv.addRule( RelativeLayout.CENTER_VERTICAL);
                tv.setLinksClickable(true);
                tv.setTextSize(0, 60);
                //tv.setLayoutParams(relTv);

                CardView cardView = new CardView(context);
                cardView.setCardBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                cardView.setRadius(10);
                cardView.setCardElevation(5);

                RelativeLayout relativeLayout = new RelativeLayout(context);
                relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                final TextView textCount = new TextView(context);
                textCount.setText(Integer.toString(count));

                RelativeLayout.LayoutParams relParam  = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
                relParam.addRule(RelativeLayout.RIGHT_OF, i);
                textCount.setTypeface(Typeface.DEFAULT_BOLD);
                textCount.setTextColor(getResources().getColor(R.color.colorPrimaryLight));
                textCount.setTextSize(0, 30);
                textCount.setPadding(0,5,5,0);
                //textCount.setLayoutParams(relParam);

                relativeLayout.addView(tv,relTv);
                relativeLayout.addView(textCount,relParam);

                cardView.addView(relativeLayout);

                //llLetters.addView(tv, lp);
                gridLayout.addView(cardView,gridParam);

                cardView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, tv.getText()+ "" + textCount.getText(), Toast.LENGTH_SHORT).show();
                    }
                });

                //llLetters.addView(gridLayout);
                //endregion
                i = i + count;
            }
            else {
                i++;
            }


        }

图片的第一个字母在左上角。我想在信的右下角显示这一点。你能帮我解决问题吗?谢谢=)

问题出在这里:

relParam.addRule(RelativeLayout.ALIGN_BOTTOM, i);
relParam.addRule(RelativeLayout.RIGHT_OF, i);

如果将零传递给 addRule,它会被解释为 false。因此你说,ALIGN_BOTTOM 在你的第一个字母中应该是 false,而 i 是零。

See the documentation:

parameter: subject, int: the ID of another view to use as an anchor, or a boolean value (represented as RelativeLayout.TRUE for true or 0 for false).