添加TextView并更新ViewGroup以显示Textviews

时间:2015-12-14 10:09:34

标签: android android-layout

我已经花了几个小时搜索和尝试,但我无法走上正确的道路。

首先是我的代码:

public class MyThreadLoc extends Thread{
Context context;
final ViewGroup view;

MyThreadLoc(Context context){
    super();
    this.context = context; 
    view = (ViewGroup) findViewById(R.id.mLayout);
}
int height;

@Override public void run() {
    Looper.prepare();   
    int i = 0;
    int y = 10;
    while(running && i < 20){ 
        i++;
        if(y >= 200){
        runOnUiThread(new Runnable() {
            public void run() {
                view.removeAllViews();
                view.setBackgroundColor(Color.BLACK);
                }
            });
        y=10;
        }
    try {
    Thread.sleep( 500 );
    Log.d("mystuff", "Sleep");  
        } catch ( InterruptedException e ) { e.printStackTrace(); }
    final ViewGroup.LayoutParams lParam = new    ViewGroup.LayoutParams(500,30); //MATCH_PARENT, 30);
        while(y < 200 && running){ 
        final float yb = y;
        runOnUiThread(new Runnable() {
            TextView tview; 
            public void run() {
            Log.d("mystuff", "innerrun");
                            synchronized (view){
                            tview = new TextView(context);
                            tview.setText("Test");

                            tview.setY(yb);
                            view.addView(tview, lParam);
                            height = tview.getHeight();
                            view.updateViewLayout(tview, new      RelativeLayout.LayoutParams(500,30));
                            Log.d("mystuff", Integer.toString(view.getChildCount()));
                             try {
                                    Thread.sleep( 50 );
                                    Log.d("mystuff", "Sleep");  
                                    } catch ( InterruptedException e ) { e.printStackTrace(); }
                            }
                        }
                    });

                 y = y + 100; //height +5;
                }

视图不会首先出现,但最终会出现2个带有&#34;测试&#34;的文本视图。出现。 如何更新它,以便我看到每个textview出现?

1 个答案:

答案 0 :(得分:0)

为什么没有帮助我解决这个问题的错误,出现得更早,所以我不需要问这个问题?

错误是:&#34;跳过300帧!应用程序可能在其主线程上做了太多工作&#34;

带我到了:The application may be doing too much work on its main thread

所以我从RunOnUiThread中移除了大部分内容,现在它可以工作了。只有2个Textviews出现,因为我有y&lt; 200设置为低,以显示更多文本视图。