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

前端 未结 3 1882
余生分开走
余生分开走 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:20

    I made a slightly different version of the accepted answer. I did not alter my layout xml in any way and did not use onTextResize() or AutoResizeTextView as that seemed an overkill for my situation. I needed my LinearLayout to switch from Horizontal orientation to Vertical orientation if the device's language setting caused a long string to be used.

    Layout

    
    
        
    
        
    
    

    Java

    private void setCustomerServiceLayoutOrientationBasedOnTextWidth() {
            TextPaint paint = customer_care_number_text.getPaint();
            TextView tvCustomerCareTitle = (TextView) findViewById(R.id.customer_care_title);
            TextView tvCustomerCareInformation = (TextView) findViewById(R.id.customer_care_information);
            int halfCustomerServiceLayoutWidth = getScreenWidth() / 2;
            boolean isCustomerCareTitleTooLong = paint.measureText(tvCustomerCareTitle.getText().toString()) > customerServiceLayoutWidth;
            boolean isCustomerCareInformationTooLong = paint.measureText(tvCustomerCareInformation.getText().toString) > customerServiceLayoutWidth;
    
            if (isCustomerCareTitleTooLong || isCustomerCareInformationTooLong) {
                LinearLayout llCustomerCareBottom = (LinearLayout) findViewById(R.id.customer_care_bottom_layout);
                llCustomerCareBottom.setOrientation(LinearLayout.VERTICAL);
            }
        }
    
    private int getScreenWidth() {
        int screenWidth;Display display = getWindowManager().getDefaultDisplay();
        if (Build.VERSION.SDK_INT < 13) {
            screenWidth = display.getWidth();
        } else {
            Point point = new Point();
            display.getSize(point);
            screenWidth = point.x;
        }
        return screenWidth;
    }
    

提交回复
热议问题