How to adjust line height in TextView with multiple type sizes?

后端 未结 4 449
既然无缘
既然无缘 2021-01-31 17:54

I have a TextView that holds a Spannable string. The string contains a bunch of text, the first word of which is double the typesize as the rest of the string.

The prob

相关标签:
4条回答
  • 2021-01-31 18:38

    You can try using one of these TextView properties:

    android:lineSpacingMultiplier
    android:lineSpacingExtra
    

    to at least try to make all lines the same height (same as the first one).

    0 讨论(0)
  • 2021-01-31 18:44

    I think you can't :(. Android os calculate the gaps between the lines according the font size, so that '30$' makes gap between line 1 and line 2 to be bigger, and I do not know if it is possible to control this gap size.

    To solve this you can easily extend LinearLayuot and put the orientation vertical. Than put 2 textviews with fillparent for width....

    and than implement function , let say setNoGapText(String s); , and in this function you can measure the text that can be fit in the textview on the top(this can me done with textutils), and than the rest of the text will be fit on textview2...

    between this textview you can play with padding and margins properties so the gaps between lines will look just like you imagine them.

    0 讨论(0)
  • 2021-01-31 18:48

    After a lot of trial and error I got a very hacky solution to work:

    1. I added spannables with the same doubled text size as the emphasized word to every space inside the text. This way the whole TextView got the same (big) line spacing. Be aware that you can not reuse a spannable.

    2. Then I gave the TextView a negative lineSpacingExtra so the line spacing looked nice again.

    3. To avoid unnatuarally wide spaces due to the increased text size I added a ScaleXSpan to every space, scaling them to 50% of their original big size.

    This method should work even for TextViews that hold localized strings (where you never know at which position the emphasized word appears or how long your line will be) as long as every line holds at least one space character.

    0 讨论(0)
  • 2021-01-31 18:51

    I wrote this function:

    INPUT

    • LinearLayout ll :

      the Layout which will be your "TexView" (make sure its orientation is vertical)

    • String money:

      the String you want to be different (in your case bigger textsize)

    • String text:

      the text

    • Context mContext

      the context

    WHAT TO DO

    I commented the parts that you must edit


    private void populateText(LinearLayout ll,
            String money, String text , Context mContext) { 
        String [] textArray = text.split(" ");
        Display display = getWindowManager().getDefaultDisplay();
        ll.removeAllViews();
        int maxWidth = display.getWidth() - 20;
    
        LinearLayout.LayoutParams params; // to be used over and over
        LinearLayout newLL = new LinearLayout(mContext);
        newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        newLL.setGravity(Gravity.LEFT);
        newLL.setOrientation(LinearLayout.HORIZONTAL);
    
        int widthSoFar = 0;
    
        ///FIRST INSERT THE MONEY TEXTVIEW
        LinearLayout LL = new LinearLayout(mContext);
        LL.setOrientation(LinearLayout.HORIZONTAL);
        LL.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM); //THIS IS IMPORTANT TO keep spacing up not down
        LL.setLayoutParams(new ListView.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
        TextView TV = new TextView(mContext);
        TV.setText(money);
        //TV.setTextSize(size);  <<<< SET TEXT SIZE
        TV.measure(0, 0);
        params = new LinearLayout.LayoutParams(TV.getMeasuredWidth(),
                LayoutParams.WRAP_CONTENT);
        //params.setMargins(5, 0, 5, 0);  // YOU CAN USE THIS
        LL.addView(TV, params);
        LL.measure(0, 0);
        widthSoFar += TV.getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
        newLL.addView(LL, params);
    
        for (int i = 0 ; i < textArray.length ; i++ ){
            LL = new LinearLayout(mContext);
            LL.setOrientation(LinearLayout.HORIZONTAL);
            LL.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
            LL.setLayoutParams(new ListView.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
            TV = new TextView(mContext);
            TV.setText(textArray[i]);
            //TV.setTextSize(size);  <<<< SET TEXT SIZE
            TV.measure(0, 0);
            params = new LinearLayout.LayoutParams(TV.getMeasuredWidth(),
                    LayoutParams.WRAP_CONTENT);
            //params.setMargins(5, 0, 5, 0);  // YOU CAN USE THIS
            LL.addView(TV, params);
            LL.measure(0, 0);
            widthSoFar += TV.getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
            if (widthSoFar >= maxWidth) {
                ll.addView(newLL);
    
                newLL = new LinearLayout(mContext);
                newLL.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                newLL.setOrientation(LinearLayout.HORIZONTAL);
                newLL.setGravity(Gravity.LEFT);
                params = new LinearLayout.LayoutParams(LL
                        .getMeasuredWidth(), LL.getMeasuredHeight());
                newLL.addView(LL, params);
                widthSoFar = LL.getMeasuredWidth();
            } else {
                newLL.addView(LL);
            }
        }
        ll.addView(newLL);
    }
    

    NOTE

    I have not tested it ... I used it for more complex things and it was working ... You might need to tweak a bit.

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