How to wrap text in textview in Android

前端 未结 18 1118
旧巷少年郎
旧巷少年郎 2020-12-08 18:03

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.

I

18条回答
  •  庸人自扰
    2020-12-08 18:55

    Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:

    LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
    childParams.setMargins(5, 5, 5, 5);
    
    Label label = new Label(this);
    label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
    label.setLayoutParams(childParams);
    

    As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.

    When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.

提交回复
热议问题