9

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

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

但是当我编写如下代码时,仅从数组列表中显示了最后一行

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);
  }
4

7 回答 7

33

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

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

custName.setText(builder.toString());
于 2013-06-26T07:01:56.157 回答
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" />
于 2013-06-26T07:00:56.680 回答
0

layout_width将您的从更改wrap_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" />
于 2013-06-26T07:02:29.720 回答
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>
于 2013-06-26T07:03:46.043 回答
0

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

String text="";
for (String details : list2) {
    text = text + details + "\n";
}
custName.setText(details);
于 2013-06-26T07:04:56.603 回答
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);
}
于 2018-10-07T04:11:32.827 回答
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);         
  }
 }

对于科特林:

   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)
    }
于 2019-12-29T03:12:40.487 回答