与九补丁textview背景的神器

时间:2012-12-12 09:04:48

标签: android textview nine-patch

我有一个TextView,其layout_width设置为wrap_content。我使用九个补丁图像作为背景。 TextView的内容会定期刷新(我使用的线程会休眠一段时间然后启动处理程序)。经过几次迭代后,有一些工件。我可以看到一些早期消息的文本和背景的一部分(它的宽度大于当前显示的消息的宽度)。可能是什么问题?

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   ...
}

private class PeriodicHelp extends Thread{
    private static final int SLEEP_TIME = 4000;
    private static final int NUM_HELP_PHRASES = 5;

    private String getHelp(){
        int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
        return helpPhrases[pos];
    }

    public void run(){
        int i = 0;
        while(true){
            try {
                Thread.sleep(SLEEP_TIME);
                Log.d(TAG, "periodicHelp " + Integer.toString(i));
                i++; //used just for debugging
                mHandler.post(new Runnable(){
                    public void run() {
                        String help = getHelp();
                        helpField.setText(help);
                    }
                });
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }
}

我在onStart()

中启动了PeriodicHelp线程

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。这是代码

public void onCreate{
   ...
   helpField = (TextView) findViewById(R.id.helpField);
   rl = (RelativeLayout) findViewById(R.id.mainView); //this RelativeLayout contains
                                                      //helpField  TextView     
   ...
}

private class PeriodicHelp extends Thread{
private static final int SLEEP_TIME = 4000;
private static final int NUM_HELP_PHRASES = 5;

private String getHelp(){
    int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
    return helpPhrases[pos];
}

public void run(){
    int i = 0;
    while(true){
        try {
            Thread.sleep(SLEEP_TIME);
            Log.d(TAG, "periodicHelp " + Integer.toString(i));
            i++; //used just for debugging

            rl.postInvalidate();//THIS LINE FIX THE PROBLEM

            mHandler.post(new Runnable(){
                public void run() {
                    String help = getHelp();
                    helpField.setText(help);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

}