有没有人知道如何在Android平台中的TextView中换行文本。即,如果textview中的文本超过屏幕长度,则应在第二行显示。
我已经搜索并尝试了以下内容:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
但是没有一个起作用...
有人能建议我该如何做吗。
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
android:layout_width="60dp"
你可以根据自己的选择更改宽度。只需输入长句以检查是否像这样工作
android:text="我希望成为世界一流的软件工程师"
我遇到了同样的问题。进行以下更改使其正常运行 -
android:layout_width="wrap_content"
我正在使用Android 2.2,如果文本超出屏幕,我的textview将自动换行。
如果您想在屏幕末尾之前使文本换行,请添加(只需放入您自己的dp值)。 如果您在文本右侧有一张图片,这将非常有用。
android:layout_marginRight="52dp"
有趣的是——我在代码中创建了TextView并且它自动换行,尽管我除了一些标准设置之外没有设置任何东西——不过你可以亲自看看:
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);
从参数定义中可以看出,我正在使用LinearLayout。Label类只是扩展了TextView - 在那里除了设置字体大小和字体颜色之外没有做任何事情。
在模拟器(API Level 9)中运行时,它会自动将文本换行成3行。
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
。很可能它会自动实现你认为的“自动换行”。 - JWLlayout_with
设置为固定大小,当文本填满最大宽度时,它会溢出到下一行,从而产生换行效果。<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"/>
关键在于textView的宽度,尝试将其设置为固定数字,例如:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
我尝试了许多解决方案,但没有任何结果,我尝试过:
android:ellipsize="none"
android:scrollHorizontally="false"
唯一触发换行选项的是专用宽度
<ScrollView android:id="@+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="@+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>
android:inputType="textPersonName"
**。显然,这是由Android Studio设计工具添加的。删除该属性解决了问题!可能还有其他输入类型也会阻止换行。 - LarsH