How to wrap text in textview in Android

前端 未结 18 1054
旧巷少年郎
旧巷少年郎 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:49

    In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.

    0 讨论(0)
  • 2020-12-08 18:54

    For me this issue only occurred on Android < 4.0

    The combination of parameters I used were:

    android:layout_weight="1"
    android:ellipsize="none"
    android:maxLines="100"
    android:scrollHorizontally="false"
    

    The maxLines count seemed to be the random final piece that made my TextView wrap.

    0 讨论(0)
  • 2020-12-08 18:54

    You must use 2 parameters :

    • android:ellipsize="none" : the text is not cut on textview width

    • android:scrollHorizontally="false" the text wraps on as many lines as necessary

    0 讨论(0)
  • 2020-12-08 18:55

    I had the same problem. Following change made it work -

    android:layout_width="wrap_content"
    

    The ellipsis, maxLines, or layout_weight - all didn't make any difference. Note - The parent width is also set as wrap_content.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 18:55

    Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.

    <TextView
        android:id="@+id/segmentText"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/segmentHeader"
        android:text="You have the option to record in one go or segments(if you swap options 
        you will loose your current recordings)"
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>
    
    0 讨论(0)
提交回复
热议问题