【问题标题】:Add TextViews and update a ViewGroup to display the Textviews添加 TextViews 并更新 ViewGroup 以显示 Textviews
【发布时间】:2015-12-14 10:09:34
【问题描述】:

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

首先是我的代码:

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 个带有“测试”的文本视图。 我怎样才能更新它,以便我看到出现的每一个文本视图?

【问题讨论】:

  • 你想做什么?

标签: android android-layout


【解决方案1】:

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

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

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

所以我从 RunOnUiThread 中删除了大部分内容,现在它可以工作了。只有 2 个 Textviews 出现,因为我将 y

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多