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

后端 未结 4 450
既然无缘
既然无缘 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题