Android TextView Text not getting wrapped

前端 未结 24 1660
时光说笑
时光说笑 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

    10dp
    

    Hope it helps.

提交回复
热议问题