如何在单个TextView中显示数组列表中的多行?

时间:2013-06-26 06:59:11

标签: android textview

我正在开发Android应用程序,因此我希望在单个文本视图中显示四行文本。 我的数组列表包含如下

之类的数据
  

客户名称:Nilesh
  客户联系方式:23230200
  客户城市:浦那

但是当我编写如下代码时,只有最后一行显示在数组List

XML

  <TextView android:id="@+id/kbpViewTvCustNm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

代码

for (String details : list2)
  {
     custName.setText(details);
  }

7 个答案:

答案 0 :(得分:27)

使用StringBuilder使您的数组成为String并在各个字符串之间附加换行符,如下所示:

StringBuilder builder = new StringBuilder();
for (String details : list2) {
   builder.append(details + "\n");
}

custName.setText(builder.toString());

答案 1 :(得分:2)

<TextView
            android:id="@+id/kbpViewTvCustNm"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

答案 2 :(得分:0)

layout_widthwrap_content更改为特定尺寸,如下所示:

<TextView
            android:id="@+id/kbpViewTvCustNm"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="8dp"
            android:lines="4"
            android:textIsSelectable="false" />

答案 3 :(得分:0)

您好,您可以在文本视图中设置最大行

  <TextView
android:id="@+id/address1"
android:gravity="left"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:maxLines="4"
android:lines="4"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

答案 4 :(得分:0)

您可以将详细信息组合为单个字符串,并使用换行符分隔:

String text="";
for (String details : list2) {
    text = text + details + "\n";
}
custName.setText(details);

答案 5 :(得分:0)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    Bundle bundle = getIntent().getExtras();
    assert bundle != null;
    ArrayList<String> expTit = bundle.getStringArrayList("expTit");
    ArrayList<Integer> expAmt =bundle.getIntegerArrayList("expAmt");
    TextView t= findViewById(R.id.textView2);
    TextView t2= findViewById(R.id.textView3);
    StringBuilder builder = new StringBuilder();
    if (expTit == null) throw new AssertionError();
    String text="";
    for (String details : expTit) {
        text = text + details + "\n";
    }
    t.setText(text);
}

答案 6 :(得分:0)

适用于整个pojo字段的简单解决方案:

for (Post post : posts) {
 String content = "";
 content += "ID: " + post.getId() + "\n";
 content += "User ID: " + post.getUserId() + "\n";
 content += "Title: " + post.getTitle() + "\n";
 content += "Text: " + post.getText() + "\n\n";

 textViewResult.append(content);         
  }
 }

对于Kotlin:

   posts.forEach {post->
        var content = ""
        content += "ID: " +  post.id + "\n"
        content += "User ID: " + post.userId + "\n"
        content += "Title: " + post.title + "\n"
        content += "Text: " + post.text + "\n\n"
      textViewResult.append(content)
    }