android textview自动换行怎么实现

2149
2024/2/4 11:02:44
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Android中,TextView默认是支持自动换行的。只需要将TextView的属性android:singleLine设置为false即可。

在XML布局文件中:

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is a long text that will automatically wrap to the next line"
    android:singleLine="false"/>

在代码中:

TextView textView = findViewById(R.id.textView);
textView.setText("This is a long text that will automatically wrap to the next line");
textView.setSingleLine(false);

这样就可以实现TextView的自动换行了。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: android多行注释如何实现