【问题标题】:multi-line textview,how to implement this layout多行文本视图,如何实现这种布局
【发布时间】:2014-08-18 20:50:52
【问题描述】:

我正在使用一个 android 应用程序。 我有两个文本视图,textView1 用于显示用户名,textView2 用于显示超过一行的单词。

布局如下:

textView2 的第二行应该与 textView1 对齐 如何实现这种布局?任何想法将不胜感激。

编辑1:实际上,我需要一个满足我需要的解决方案,如果需要,您可以使用两个以上的文本视图。

编辑 2:Ozi 的解决方案确实解决了我的问题,但是如果我以后需要在 textView1 上绑定一个监听器怎么办?还有其他解决方案吗?谢谢。

【问题讨论】:

  • 使用相对布局并对齐您的视图,您甚至可以使用编辑器的 GUI。
  • stackoverflow.com/questions/2248759/… 在这里你可以看到如何使用flowtexthelper。原理是一样的,你的textview会环绕第一个view

标签: android android-layout


【解决方案1】:

您可以对所有文本使用 1 个文本视图。你应该像这样改变黑色部分的样式:

   String blackstring = "<font color='#000000'>black</font>";
   t.setText(Html.fromHtml(blackstring + maintext));

【讨论】:

  • 好的,非常感谢。这可以解决我的问题。但是如果我以后需要在 textView1 上绑定一个监听器呢?
  • @cloud,然后在 TextView 内容中实现对单词的链接。
  • @cloud 好问题,我认为 textview 没有部分文本监听器
  • @Haresh 我不太明白你在说什么,你能在下面给我一个详细的答案吗?谢谢
  • @cloud 现在我认为有一种方法可以链接一个单词:stackoverflow.com/questions/9969789/…
【解决方案2】:

你可以使用html:

txt.setText(Html.fromHtml(setText("username", "some long text")));  

private String setText(String title, String message) {
    return "<span>" + title + ": </span>$nbsp &nbsp &nbsp" + message;
}  

您也可以设置 align=justify。
this link。我认为这是您问题的答案

【讨论】:

    【解决方案3】:

    我认为您可以通过将两个文本视图堆叠在一起(一个在另一个之上)来简单地实现这一点,并固定黑色文本视图的宽度。然后,您只需在需要为其设置文本时,在红色 textview 前面添加一些空白空间。

    【讨论】:

      【解决方案4】:
      Try this layout:
      <TableLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:stretchColumns="1" >
      
              <TableRow
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" >
      
                  <TextView
                      android:id="@+id/textView1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_vertical"
                      android:background="#cecece"
                      android:text="TextView"
                      android:textColor="#000000" />
      
                  <TextView
                      android:id="@+id/textView2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_gravity="center_vertical"
                      android:background="#FF0000"
                      android:text="TextView"
                      android:textColor="#000000" />
              </TableRow>
      
              <TableRow
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" >
      
                  <TextView
                      android:id="@+id/textView3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_span="2"
                      android:background="#FF0000"
                      android:text="TextView TextView TextView TextView"
                      android:textColor="#000000" />
              </TableRow>
          </TableLayout>
      

      【讨论】:

      • 感谢您的回答,但有一个问题是我怎么知道 textview2 应该有多长?因为 textview2 和 textview3 实际上是同一个字符串,只是不同的部分。
      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2020-09-06
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多