Java android:使用TextView追加换行符

问题描述

| 我只想以某种方式向我的线性布局添加新行:
layout = (LinearLayout) findViewById (R.id.layout);  

... //some other code where I\'ve appended some strings already

final TextView nline = new TextView(this);
nline.setText(Html.fromHtml(\"<br>\")); //i also tried:  nline.setText(\"\\n\");
layout.addView(nline);
但这只会增加一些空间。有人可以帮我吗?谢谢。     

解决方法

        如果只想在其他两个视图之间留出一些空白,则可以在XML中进行此操作(假设您使用XML作为布局)。这样的事情可能会起作用,基本上是将View设置为透明背景并指定高度。假设您在TextViews中具有所需的任何参数。
<TextView />

<View android:background=\"#00000000\"
      android:layout_height=\"12dp\" //or whatever density pixel height you want
      android:layout_width=\"fill_parent\" />

<TextView />
另外,在上面的尝试中,您可以尝试使用空格和换行符,这可能会起作用。
nline.setText(\" \\n\");
    ,        首先,您需要使ѭ3成为多行。然后使用简单的
\"\\n\"
字符串进行换行。
final TextView nline = new TextView(this);
nline.setSingleLine(false);
nline.setText(\"first line\\n\"+\"second line\\n\"+\"third line\");
    ,        您可能需要使用TextView的setInputType()方法将InputType设置为TYPE_TEXT_FLAG_MULTI_LINE
tv.setInputType(tv.getInputType()|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
您还可以在其他视图上设置一些边距/填充。我认为不应将TextViews用作间隔符。     ,        简单为:
String hello = getResources.getString(R.string.hello);
String world = getResources.getString(R.string.world);
textView.setText(hello + \"\\n\" + world);
    ,        只需给出\“ \\ n \”即可在新行中显示数据
txtView.setText(\"Latitude :\" + latitude + \"\\nLongitude :\" + longitude);