Multiline TextView in Android?

前端 未结 20 1334
南方客
南方客 2020-11-29 17:26

I did like below in xml


    

        
相关标签:
20条回答
  • 2020-11-29 18:06
    You can do this with TableRow, see below code
    
    <TableRow >
        <TextView
            android:id="@+id/tv_description_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="8dp"
            android:text="@string/rating_review"
            android:textColor="@color/black"
            android:textStyle="bold" />
    
        <TextView
            android:id="@+id/tv_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:maxLines="4"`enter code here`
            android:padding="8dp"
            android:text="The food test was very good."
            android:textColor="@color/black"
            android:textColorHint="@color/hint_text_color" />
    </TableRow>
    
    0 讨论(0)
  • 2020-11-29 18:06

    Simplest Way

    <TableRow>
        <TextView android:id="@+id/address1"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:gravity="left"
            android:maxLines="4" 
            android:singleLine="false"              
            android:text="Johar Mor,\n Gulistan-e-Johar,\n Karachi" >
        </TextView> 
    </TableRow>
    

    Use \n where you want to insert a new line Hopefully it will help you

    0 讨论(0)
  • 2020-11-29 18:07

    TextView will be multi line when it wont get enough space to fit in the single line and singLine not set to true.

    If it gets the space in one line it wont be multi line.

    0 讨论(0)
  • 2020-11-29 18:08

    Try to work with EditText by make it unclickable and unfocusable, also you can display a scrollbar and delete the EditText's underbar.
    Here is an example:

    <EditText
    android:id="@+id/my_edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_parent"
    android:inputType="textMultiLine"                   <!-- multiline -->
    android:clickable="false"                         <!-- unclickable -->
    android:focusable="false"                         <!-- unfocusable -->
    android:scrollbars="vertical"     <!-- enable scrolling vertically -->
    android:background="@android:color/transparent"   <!-- hide the underbar of EditText -->
    />  
    

    Hope this helps :)

    0 讨论(0)
  • 2020-11-29 18:09

    Why don't you declare a string instead of writing a long lines into the layout file. For this you have to declare a line of code into layout file 'android:text="@string/text"' and go to the \\app\src\main\res\values\strings.xml and add a line of code ' Your multiline text here' Also use '\n' to break the line from any point else the line will automatically be adjusted.

    0 讨论(0)
  • 2020-11-29 18:10

    TextView is used to display text on Android application. By default, TextView displays text on one line and if long, TextView will automatically display with more lines to display its text in the most logical way.

    Android developers can create a new line on TextView both in programming and syntax. Android developers can create multi-line TextView without dividing text into multiple lines according to android: minLines properties. Create layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        android:orientation="vertical">
    
        <TextView
    
            android:id="@+id/text_view1"
    
            android:layout_width="match_parent"
    
            android:layout_height="wrap_content"
    
            android:textSize="30sp"
    
            android:background="#DA70D6"
    
            android:text="This is line 1 \nThis is line 2 \nLine number 3"
    
            />
    
    
    
        <TextView
    
            android:id="@+id/text_view2"
    
            android:layout_width="match_parent"
    
            android:layout_height="wrap_content"
    
            android:textSize="30sp"
    
            android:background="#DEB887"
    
            android:text="This is line 1 \nThis is line 2 \nLine number 3"
    
            android:maxLines="2"
    
            />
    
    
    
        <TextView
    
            android:id="@+id/text_view3"
    
            android:layout_width="match_parent"
    
            android:layout_height="wrap_content"
    
            android:textSize="30sp"
    
            android:background="#8FBC8F"
    
            android:text="This is line 1 \nThis is line 2"
    
            android:minLines="3"
    
            />
    
    
    
        <TextView
    
            android:id="@+id/text_view4"
    
            android:layout_width="match_parent"
    
            android:layout_height="wrap_content"
    
            android:textSize="30sp"
    
            android:background="#5F9EA0"
    
            android:text="@string/Multiline_Text_By_N"
    
            />
    
    </LinearLayout>
    

    Add string in file string.xml

    <string name="Multiline_Text_By_N">
    
            Line number 1 \nLine number 2
    
        </string>
    

    Source: https://code-android-example.blogspot.com/2020/11/android-textview-multiline-example.html

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