Move two side by side textviews to be one under another if text is too long

前端 未结 3 1866
余生分开走
余生分开走 2021-01-14 07:36

I have two textviews like this:

=======================
= TextView1 TextView2 =
=======================

And I would like to detect when the

3条回答
  •  时光取名叫无心
    2021-01-14 08:36

    I have found a better solution. Changed my textviews into autoresizable textviews (more info here)

    Also, each textview is in a separate layout, to make sure both textviews are resized to the same value. My xml looks like this:

    
    
        
            
        
    
        
    
            
        
    
    
    

    and I have implemented the OnTextResizeListener from AutoResizeTextView to do this:

    public class TextWidthResizeListener implements OnTextResizeListener {
    
        @Override
        public void onTextResize(TextView textView, float oldSize, float newSize) {
            TextPaint paint = textView.getPaint();
            if (paint.measureText(textView.getText().toString()) > (valueLinearLayout.getWidth() / 2)){
                valueLinearLayout.setOrientation(LinearLayout.VERTICAL);
            }
        }
    }
    

    where valueLinearLayout is:

    valueLinearLayout = (LinearLayout)findViewById(R.id.value_linear_layout);
    

    This solution best fits for me, as the textviews are dimensioned when they are side by side until a minimum size. When the minimum size is reached, and the text still does not fit, the textviews will be aligned one under another.

    Also, this idea with the listener can be applied to non-resizable textviews also. I will set this answer as the correct one.

提交回复
热议问题