网格布局的第一项有什么问题?

在此代码中,我能够创建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);
                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,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++;
            }


        }

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

网格布局的第一项有什么问题?

cangmings 回答:网格布局的第一项有什么问题?

问题出在这里:

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

如果将零传递给addRule,它将被解释为false。因此,您说ALIGN_BOTTOM为零时,第一个字母false应该为i

See the documentation:

  

参数:subject,int:用作锚点的另一个视图的ID,或一个布尔值(表示为RelativeLayout.TRUE表示true,0表示false)。

本文链接:https://www.f2er.com/3048060.html

大家都在问