【问题标题】:Android TextView doesn't do linebreak in ListView (multiline text)Android TextView 不在 ListView 中进行换行(多行文本)
【发布时间】:2013-02-14 02:48:17
【问题描述】:

我创建了一个 3 级 ExpandableListView,但如果内容太长,用于 2 级和 3 级的 TextView 不支持换行符的问题。如果需要,它应该动态超过一行。第一级TextView(自动)做得很好,实际上我在xml中对所有三个TextView都有相同的设置。接下来是布局xml,id为groupname的TextView用于第二层(例如下图中的第一个红色X),id为childname的TextView用于第三层(例如第二个和第三个红色X)下图)。应该都像图中的绿色钩子那样。

"singleLine=false" 似乎不起作用。还尝试了其他 SO 帖子中的一些不同选项,但我测试的内容对我没有用。像 ellipsize、scroll Horizo​​ntale、不同的 layout_width 等等。唯一有效的方法是在 x 百 dp 上设置一个固定的 layout_width,但这不是动态的,对吗?

如果有人可以帮助我,那就太好了。非常感谢!

截图如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/childname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="60dp"
    android:layout_marginLeft="60dp"
    android:textColor="#AAAAAA"
    android:singleLine="false"
    android:text=""
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/groupname"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="45dp"
    android:layout_marginRight="60dp"
    android:textColor="#555555"
    android:singleLine="false"
    android:gravity="center_vertical"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium" />

【问题讨论】:

    标签: android listview textview expandablelistview multiline


    【解决方案1】:

    在你的 xml 中添加这一行

    android:inputType="textMultiLine"
    

    使用这样的编码添加文本,您可以在其中使用'\n'添加换行符(但在这里您必须手动添加所需的换行符)

    TextView txt1 = (TextView) findViewById(R.id.childname);
    txt1.setText("Hi \nHello \nHow are You");
    

    结果将是


    你好
    你好吗

    编辑 接受的答案 - 删除行 'android:layout_alignParentLeft="true"

    【讨论】:

    • 谢谢,但这两者都不是正确的方法。 inputType 行会导致警告,它仅适用于 TextEdits,而不适用于 TextViews。无论如何都试过了,它并没有改变什么。手动中断与 \n 不是选项,TextView 的文本稍后由 sqlite db 中的数据设置,并且此数据经常更改以进行修复。我不知道为什么第一层的行包裹得很好,而其他的则没有。我添加了 textcolor 选项来检查执行时是否有任何变化,这很有效。但不是换行符,虽然我读到 singleLine="false" 也是默认值。更多想法?
    • 您是否尝试删除行 'android:layout_alignParentLeft="true"' 并尝试?
    【解决方案2】:

    尝试使用 LinearLayout 而不是 RelativeLayout 作为 TextView

    的父级

    【讨论】:

      【解决方案3】:

      我已将此属性添加到 ListView 内的 TextView 中,并使其正确换行。

      android:maxWidth="xxxdp"
      

      F.Y.R.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 1970-01-01
        • 1970-01-01
        • 2014-06-05
        相关资源
        最近更新 更多