Multiline TextView with width “wrap_content”

后端 未结 4 2251
南方客
南方客 2021-02-18 21:58

I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.

    

        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-18 22:42

    I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.

    I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.

提交回复
热议问题