Android TextView Text not getting wrapped

前端 未结 24 1439
时光说笑
时光说笑 2020-11-27 02:51

Can anyone tell me what\'s going wrong with the text? Text longer than one line doesn\'t wrap to the next line but goes beyond the screen.

Following is the code:

相关标签:
24条回答
  • 2020-11-27 03:33

    I finally managed to add some pixels to the height of the TextView to solve this issue.

    First you need to actually get the height of the TextView. It's not straightforward because it's 0 before it's already painted.

    Add this code to onCreate:

    mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt);
    if (mReceiveInfoTextView != null) { 
        final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int height = mReceiveInfoTextView.getHeight();
                int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height);
                mReceiveInfoTextView.setHeight(height + addHeight);
    
                // Remove the listener if possible
                ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver();
                if (viewTreeObserver.isAlive()) {
                    viewTreeObserver.removeOnGlobalLayoutListener(this);
                }
            }
        });
    }
    

    You need to add this line to dimens.xml

    <dimen name="view_add_height">10dp</dimen>
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-27 03:34

    I know, that in question it is correct, but in my case, the problem was in setting textSize property in 'dp' - I've changed it to 'sp' and it works fine.

    0 讨论(0)
  • 2020-11-27 03:35

    For my case removing input type did the trick, i was using android:inputType="textPostalAddress" due to that my textview was sticked to one line and was not wrapping, removing this fixed the issue.

    0 讨论(0)
  • 2020-11-27 03:36

    I'm using constraint layout mostly.

    1) android:layout_width="match_parent" = tries to stretch to meet edges

    2) android:layout_width="wrap_content" = based solely on the input text without regard for other views nearby. for example adding android:textAlignment="center" will change the shape of the text

    3) android:padding="12dp" android:layout_width="0dp" android:layout_weight="1" android:singleLine="false"

    = The text will fold to accommodate nearby layouts without regard to the text itself

    0 讨论(0)
  • 2020-11-27 03:39

    I think it depends on the particular combination of layouts in your display. Some flags may get overridden or ignored. I have a TabHost with tabs, each tab is a list of tables. So it is a tab of ListView, each row being a TableLayout of TextView. I tried the fixes listed above and none of them worked.

    0 讨论(0)
  • 2020-11-27 03:39

    To wrap the text and to put the text in next line we sholud use the "\n" i.e new line character in the layout xml file and check tht change on the emulator not on the layout screen.

    0 讨论(0)
提交回复
热议问题