1

我创建了一个测试 android 应用程序,它侦听 RabbitMQ 服务器上的主题并在文本视图中显示消息。

一切正常,除了在添加到“输出”文本视图的每条新消息上,新文本都被绘制在旧文本上。当我滚动时也会发生同样的情况,如第二张图片所示。当应用程序进入后台并再次激活时,文本再次正常显示(见图1)。

正常输出

输出乱码

我的布局很简单:我做错了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="60dp" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Connection status" />

<TextView
    android:id="@+id/status"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:saveEnabled="false"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Received messages:" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/output"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:saveEnabled="false"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView>

这是我处理新消息的地方:

final Handler myViewUpdateHandler = new Handler() {
    // @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case NEWSTATUS: 
            status.setText((String) msg.obj);
            status.invalidate();
            //also add to output
        case NEWMESSAGE:
            String oldText= (String) output.getText();
            output.setText("");

            String newText = DateUtil.getClockTimeMillis()+" "+(String) msg.obj+"\n"+oldText;

            output.setText(newText);
            output.invalidate();
            break;
        default:
            throw new IllegalArgumentException("no case for:" + msg.arg1);
        }
        super.handleMessage(msg);
    }
};
4

2 回答 2

1

我有同样的问题。就我而言,这就是发生的事情以及我是如何解决的:

  • 首先,我添加了具有全屏属性的第二个活动(通过 eclipse 向导)

  • 然后我决定不使用那种花哨的全屏动画,所以我
    编辑了布局 XML 文件并将其更改为正常的相对视图。

  • 应用清单文件表明此活动仍在使用全屏主题。

  • 我已经删除了这些行并且...问题解决了。

如果您为布局设置全屏主题,请尝试将其更改为应用程序主主题。

于 2013-07-23T08:04:35.143 回答
0

尝试从所有情况下删除 invalidate() 方法调用,如果它不能解决问题,请在您的 xml 中将其放入 ScrollView 属性的 TextView(id=output)

 android:cacheColorHint="#00000000"
于 2013-04-25T07:33:22.100 回答