【问题标题】:what is wrong in this first item in gridlayout?gridlayout 中的第一项有什么问题?
【发布时间】:2020-03-18 23:38:40
【问题描述】:

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


        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++;
            }


        }

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

【问题讨论】:

  • 问题出在第一个A 并且已经解决。不过谢谢

标签: android android-cardview android-gridlayout


【解决方案1】:

问题出在这里:

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

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

See the documentation:

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

【讨论】:

  • 在第一行,i 应该是RelativeLayout.TRUE。在第二秒内,i 应该是它必须对齐的视图的id。
  • i 实际上是一个 id,但它是动态的。儿子当我i+1 解决了这个问题。 ;)
猜你喜欢
  • 2020-09-17
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
相关资源
最近更新 更多