【问题标题】:Force line break for each word为每个单词强制换行
【发布时间】:2016-02-09 16:35:51
【问题描述】:

我有 "strings" = word 表示放入名为 "bWord" 的数组中的字符以将字符放入按钮中,问题是空格 " " 被认为是一个字符并且也放入了一个按钮中。我不希望字符串 " " 来创建断行而不是调用 bWord

Book Cover 之类的单词会如下所示: [B][O][O][K][ ][C][O][V][E][R]

我希望它看起来像

[B][O][O][K]

[C][O][V][E][R]

这是当前问题的图片:

有人告诉我要创建一个新布局,但我不知道该怎么做

这是我正在使用的代码

        // draw word buttons

    RelativeLayout wordLayout = (RelativeLayout) findViewById(R.id.wordlayout);
    String[] word = ld.getLevelSpecific().getProcessedWord();

    for (int i = 0; i < word.length; i++) {
        Button temp = new Button(this);
        temp.setText("");
        temp.setTypeface(null, Typeface.BOLD);
        temp.setTextSize(TypedValue.COMPLEX_UNIT_SP, isd.getWordTextSize());
        temp.setIncludeFontPadding(false);
        temp.setId(idCount * 2 + i); 
        temp.setTag(Integer.valueOf(i));
        temp.setBackgroundResource(R.drawable.button_shape_letter);
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                (int) (isd.getWordButtonSize() * orm.getScaleRatio()-2),
(int) (isd.getWordButtonSize() * orm.getScaleRatio()+6*orm.getScaleRatio()));

        if (i > 0) {
            p.addRule(RelativeLayout.RIGHT_OF, idCount * 2 + i - 1);
        } else {
            p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        }


        p.setMargins(isd.getExtraspaceWord()+1, isd.getExtraspaceWord(),
                isd.getExtraspaceWord()+1, isd.getExtraspaceWord());
        temp.setLayoutParams(p);
        bWord[i] = temp;
        temp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                onWordPressed((Integer) v.getTag());

            }
        });
        wordLayout.addView(temp);
    }

【问题讨论】:

    标签: java android android-layout newline android-relativelayout


    【解决方案1】:

    我不确定我是否正确理解了您的问题。

    你提到了“Book Cover”这个词,但你的截图显示的是“Bill Clinton”......

    我做了一个修复显示:

    [B][I][L][L]

    [C][L][I][N][T][O][N]

    代替:

    [B][I][L][L][ ][C][L][I][N][T][O][N]

    如何解决:

    选项 1:

    您可以用两个 LinearLayout 替换您的 RelativeLayout。例如 Line1 和 Line2。

    然后,在运行时,您调用:

    LinearLayout Line1 = (LinearLayout) findViewById(R.id.line1);
    LinearLayout Line2 = (LinearLayout) findViewById(R.id.line2);
    ...
    if(word[i].equals(" "))
        insertingInFirstLine = false;
    ...
    if(insertingInFirstLine)
        Line1.addView(newButton);
    else 
        Line2.addView(newButton);
    

    优点是您无需担心视图 ID。只需创建具有水平方向的线性布局并将它们放在另一个下方。

    当单词只有一行时,可以将 Line2 的可见性设置为“View.GONE”

    选项 2:

    如果您真的需要使用相对布局,我更新了您的“FOR”循环。这样,在空格字符(“”)之后,所有新视图都将添加到第一行下方。 下面是例子:

    int lineDivider = 1000;
    for (int i = 0; i < word.length; i++) {
        if(word[i].equalsIgnoreCase(" ")) {
            lineDivider = i;
        } else {
            Button temp = new Button(this);
            ...
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(                        
                        (int) (isd.getWordButtonSize() * orm.getScaleRatio()-2), 
                        (int) (isd.getWordButtonSize() * orm.getScaleRatio()+6*orm.getScaleRatio()));
    
            if (i > 0) {
                if(i < lineDivider) {
                    // First Line Items
                    p.addRule(RelativeLayout.RIGHT_OF, idCount * 2 + i - 1);
                } else if (i == lineDivider + 1) {
                    // First Item of Second Line
                    p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    p.addRule(RelativeLayout.BELOW, idCount * 2);
                } else {
                    // Second Line Items
                    p.addRule(RelativeLayout.BELOW,  idCount * 2);
                    p.addRule(RelativeLayout.RIGHT_OF, idCount * 2 + i - 1);
                }
            } else {
                // First ITEM
                p.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            }
            ....
        }
    }
    

    【讨论】:

    • 谢谢,我尝试使用此代码,但它显示重复的局部变量“i”错误,由 lineDivider = i; 引起我也在答案部分下的字母池中使用“i”变量,在那里你可以看到那个 facebook 按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2014-10-27
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多