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

问题描述 投票:0回答:1

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


        }

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

image

android android-cardview android-gridlayout
1个回答
0
投票

问题出在这里:

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

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

See the documentation:

如果规则是相对于布局方向(例如RelativeLayout.START_OF)的,则在使用绝对规则(例如RelativeLayout.LEFT_OF)调用getRule(int)之前,必须使用resolveLayoutDirection(int)解析布局方向。

© www.soinside.com 2019 - 2024. All rights reserved.