我创建了一个 3 级 ExpandableListView,但如果内容太长,用于第 2 级和第 3 级的 TextView 不支持换行符。如果需要,它应该动态地超过一行。第一级 TextView 做得很好(自动),实际上我在所有三个 TextView 的 xml 中都有相同的设置。接下来是layout xmls,id为groupname的TextView是2级的(比如下图中第一个红色的X),id为childname的是3级的(比如图中的第二个和第三个红色X)下图)。应该都是图中绿色钩子(Hook)处的样子。
“singleLine=false”似乎不起作用。还尝试了在其他 SO 帖子中找到的一些不同选项,但我测试过的选项对我没有用。像 ellipsize,scroll horizo nale,不同的 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" />
最佳答案
在您的 xml 中添加这一行
android:inputType="textMultiLine"
或
使用这样的编码添加文本,您可以使用'\n'添加换行符(但在这里您必须在需要的地方手动添加换行符)
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");
结果会是
你好
你好
你好吗
编辑 接受的答案 - 删除行 'android:layout_alignParentLeft="true"
关于Android TextView 不在 ListView 中做换行(多行文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14867013/