Android - 创建动态TextView

时间:2012-10-16 16:44:17

标签: java android xml textview

我正在创建一款Android游戏,并且使用TextView时出现动态文本问题。在我的布局中,我有一个TextView,在相对布局中有足够的空间用于多行。

我想要做的是在TextView中添加5行,其功能是一旦尝试编写第6行,它会自动覆盖第1行,因此只显示最多5行文本。

我所追求的例子:

dynamic line 1
...
dynamic line 5

请在下面找到我的xml代码:

<RelativeLayout android:id="@+id/battleconsole" 
    android:layout_width="fill_parent"
    android:layout_height="135dp"
    android:gravity="center_horizontal"
    android:layout_below="@+id/spacer1" >

    <TextView
            android:id="@+id/battle_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Console with information" />
 </RelativeLayout>

这是我用来更新TextView中文本的代码:

TextView update = (TextView)findViewById(R.id.battle_details);
update.setText("test console data going in here");

我不确定使用TextView是否可以实现这一点,如果没有其他方法我可以解决这个问题?

提前致谢

1 个答案:

答案 0 :(得分:3)

将5行保留在集合(数组或列表)中。在TextView上设置文本时,请在“\ n”上加入集合中的行。然后只需用更新的文本替换集合中的项目。

Android在TextUtils类中有一个join方法,但我更喜欢Guava库的Joiner类。看看:Google Guava