2013-02-14 136 views
2

我已经创建了3级ExpandableListView,并且存在问题,即用于第二级和第三级的TextViews不支持换行符,如果内容是太长。如果需要,它应该在多行上动态显示。第一级的TextView可以自动完成,并且我实际上在所有三个TextView的xml中都有相同的设置。接下来是布局xmls,其中id为groupname的一个TextView用于第二级别(例如下图中的第一个红色X),id为childname的那个用于第三级别(例如,第二级别中的第二个和第三个红色X下图)。它应该都像在图片中的绿色钩子。Android TextView不会在ListView中执行换行符(多行文本)

“singleLine = false”似乎不起作用。也尝试了其他SO帖子中发现的一些不同的选项,但我已经testet没有为我工作。像ellipsize一样,滚动horizo​​ntale,不同的layout_width等等。唯一能做的就是在xdp上设置一个固定的layout_width,但这不是动态的,我是对的?

如果有人可以帮助我,这将是伟大的。很感谢!

以下是截图:

enter image description here

<?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" /> 

回答

5

在XML

android:inputType="textMultiLine" 

添加TEX添加此行T使用编码这样的,在这里你可以使用“\ n”添加换行符(但在这里,你必须手动添加您想要的地方休息)

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

结果将是


你好
你怎么

编辑 接受的答案 - 删除行“机器人:layout_alignParentLeft =“真”

+0

Thx,但它不是正确的方式。 inputType行会导致一个警告,即它只针对TextEdits,而不针对TextView。无论如何都试过了,它没有改变什么。手动中断\ n是没有选择的,TextView的文本稍后由sqlite数据库中的数据设置,并且这些数据经常变化以进行修复。我不知道为什么第一级的行被包好,其他人没有。我已经添加了textcolor选项来检查在执行时是否有任何改变,并且这是有效的。但不是linebreak,虽然我读singleLine =“false”也是默认的。更多的想法? – 2013-02-14 03:31:17

+0

您是否尝试删除该行 'android:layout_alignParentLeft =“true”' 并尝试? – 2013-02-14 03:37:03

+1

Thx Batz,没有这条线它适合我。 – 2013-02-21 21:17:57

0

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

1

我有这个属性添加到我里面的ListView的TextView,并使其不换行正确。

android:maxWidth="xxxdp" 

F.Y.R.