本文介绍了重新考虑layerLayout中的textArea growByLimit问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将textArea setGrowByContent设置为true,并将setGrowLimit设置为2,但是始终只有一行。所以我扭曲了代码,如果textArea的大小大于Button的大小,则将textArea的
高度增加了两倍。以下是代码。我的问题在问题的结尾:

I set the textArea setGrowByContent true and setGrowLimit to 2, but there is always only one row. So i twisted the code and increase the height of textArea by twice if the size of textArea is greater than Button size. Below is the codes. My issue is at the end of the question:

      Button homeButtonn = new Button(btnIcon){

      @Override
      protected Dimension calcPreferredSize() {
      System.out.println("Button size: " + super.calcPreferredSize());
      return super.calcPreferredSize(); 
      }

      @Override
      public void setUIID(String id) {
          super.setUIID("homeButtonn"); 
          }
      };

      TextArea buttonTitle = new TextArea(title){

      @Override
           protected Dimension calcPreferredSize() {
           System.out.println("textArea title: " + getText());
           System.out.println("Textarea size: " +super.calcPreferredSize());
           System.out.println("");
           return super.calcPreferredSize(); 
           }
      };
      buttonTitle.setUIID("smallLabel");
      buttonTitle.getAllStyles().setAlignment(Label.LEFT);
      zeroPaddingMargin(buttonTitle);                   

      if (buttonTitle.getPreferredW() -10 > homeButtonn.getPreferredW()) {
         buttonTitle.setPreferredH(buttonTitle.getPreferredH() * 2);
      }
      buttonTitle.setPreferredW(homeButtonn.getPreferredW() - 10);
      buttonTitle.getAllStyles().setMargin(3, 3, 3, 3);
      buttonTitle.setEditable(false);
      buttonTitle.setGrowByContent(true);
      buttonTitle.setGrowLimit(2);
      buttonTitle.setScrollVisible(false);
      gridContainer.add(LayeredLayout.encloseIn(homeButtonn, FlowLayout.encloseRightBottom(buttonTitle)));  

输出:

Button size: width = 146 height = 140
textArea title: DJ and Sound
Textarea size: width = 194 height = 25

这里textArea的大小大于按钮的大小,但是如果我将按钮的确切大小设置为textArea的大小,则
非常合适。那么,textArea的大小如何大于按钮的大小,并且textArea的所有文本也可以很好地适合按钮内部?
我遇到的问题是,由于textArea的大小大于按钮的大小,因此textArea的高度乘以其自身高度的两倍,但是
textArea非常适合单行,并且在下面留下了多余的行/行。

Here textArea size is greater than button size but if i set size of the textArea with exact size of button,it fits well. So how can textArea size be greater than button size and also all texts of textArea fits well inside button?The problem i got is that since textArea size is greater than button size, the height of textArea is multiplied by twice its own height but textArea fit well in single line and it leaves extra line/row below.

PS参见屏幕截图。谢谢

PS see the screenshot. Thankyou

没有calcPreferred size或preferredSize:

without calcPreferred size or preferredSize:

如果删除了calcPreferredSize,它将占用整个屏幕宽度

If calcPreferredSize is removed, it takes whole screenwidth though it is in gridlayout with 3 column

更新:
最近的代码,但没有calcPreferredSize& textArea也嵌套在容器中

Update:Recent Code without calcPreferredSize & textArea nested in a container too

GridLayout gl2 = new GridLayout(counter / 3 + 1, 3);
gl2.setAutoFit(true);
Container gridContainer = new Container(gl2);
gridContainer.setScrollableY(true);
f.addComponent(gridContainer);  

imageUrl = entry.get("img").toString();
title = entry.get("name").toString();
homePlaceholder = homePlaceholder.scaled(screenWidth / 3 - 20, screenWidth / 3 - 26);
encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder, true);
Image btnIcon = URLImage.createToStorage(encodedHomePlaceholder, "home_" + title + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE_TO_FILL);
homeButton.setIcon(btnIcon);
TextArea buttonTitle = new TextArea(title);
buttonTitle.getAllStyles().setAlignment(Label.LEFT);
buttonTitle.getAllStyles().setMargin(3, 3, 3, 3);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
Container btnTitleContainer = new Container();
btnTitleContainer.addComponent(buttonTitle);
gridContainer.add(LayeredLayout.encloseIn(homeButton, FlowLayout.encloseRightBottom(btnTitleContainer)));
f.revalidate();


推荐答案

我所做的一切都是为了使其在图层布局中正确无误和textarea,它没有用,所以我尝试了其他方法,但是它起作用了。我用边框布局&设置容器的bg图像

What ever i did to make it right in layer layout and textarea, it didnt work so i tried other things and it work. I set the bg image of the container with border layout & keep the textarea in the south of borderlayout.

homePlaceholder = homePlaceholder.scaled(screenWidth / 3, screenWidth / 3);
encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder, true);
Image btnIcon = URLImage.createToStorage(encodedHomePlaceholder, "home_" + title + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE_TO_FILL);

Container innerContainer = new Container(new BorderLayout());
innerContainer.getAllStyles().setBgImage(btnIcon);
innerContainer.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED);
gridContainer.add(innerContainer);

TextArea buttonTitle = new TextArea(properCase(title));
buttonTitle.setUIID("smallLabel");
zeroPaddingMargin(buttonTitle);
innerContainer.add(BorderLayout.SOUTH,buttonTitle);

buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);

gridContainer.revalidate();

这篇关于重新考虑layerLayout中的textArea growByLimit问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:09