首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android TextView在ListView (多行文本)中不做换行符

问Android TextView在ListView (多行文本)中不做换行符
EN

Stack Overflow用户
提问于 2013-02-14 10:48:17
回答 3查看 6.3K关注 0票数 2

我已经创建了一个三级ExpandableListView,如果内容太长,用于第二级和第三级的TextViews不支持换行符。如果需要,它应该动态地跨越多行。第一级TextView (自动)做得很好,实际上我在xml中对所有三个TextViews都有相同的设置。紧随其后的是布局TextView,一个带有id groupname的xmls是用于第二级的(例如,下图中的第一个红色X),而一个带有id TextView with的xmls是用于第三级的(例如,下图中的第二个和第三个红色X)。这一切都应该像图片中的绿色钩子一样。

"singleLine=false“似乎不起作用。我也尝试了在其他SO帖子中找到的一些不同的选项,但我所测试的对我来说并不起作用。如椭圆大小,滚动水平故事,不同的layout_width等。唯一有效的方法是在x个dp上设置一个固定的layout_width,但这不是动态的,我说的对吗?

如果有人能帮我的话就太好了。非常感谢!

这是一个截图:

​

​

代码语言:javascript
复制
<?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" />

​

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-14 11:08:46

在xml中添加下面这一行

代码语言:javascript
复制
android:inputType="textMultiLine"

或

使用如下代码添加文本,您可以使用‘\n’添加换行符(但在这里您必须手动在您想要的位置添加换行符)

代码语言:javascript
复制
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");

结果将是

Hi

问候

How are (你好吗?)

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

票数 5
EN

Stack Overflow用户

发布于 2013-02-14 12:33:28

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

票数 1
EN

Stack Overflow用户

发布于 2013-06-25 17:25:34

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

代码语言:javascript
复制
android:maxWidth="xxxdp"

仅供参考。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14867013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档