代码之家  ›  专栏  ›  技术社区  ›  lbedogni

添加视图后刷新线性布局

  •  13
  • lbedogni  · 技术社区  · 16 年前

    我正在尝试向linearlayout动态添加视图。 我通过getChildCount()看到视图被添加到了布局中,但是即使对布局调用invalidate(),也不会显示childs。

    3 回复  |  直到 16 年前
        1
  •  22
  •   Klarth    16 年前

    您可以在代码中签入以下内容:

    TextView 在启动时短暂延迟后:

    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class ProgrammticView extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final LinearLayout layout = new LinearLayout(this);
            layout.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.FILL_PARENT));
    
            setContentView(layout);
    
            // This is just going to programatically add a view after a short delay.
            Timer timing = new Timer();
            timing.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    final TextView child = new TextView(ProgrammticView.this);
                    child.setText("Hello World!");
                    child.setLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.FILL_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));
    
                    // When adding another view, make sure you do it on the UI
                    // thread.
                    layout.post(new Runnable() {
    
                        public void run() {
                            layout.addView(child);
                        }
                    });
                }
            }, 5000);
        }
    }
    
        2
  •  2
  •   Daniel    15 年前

    我也遇到同样的问题,并注意到在invalidate之后没有调用重写的onMeasure()方法。所以我在LinearLayout中创建了自己的子例程,并在invalidate()方法之前调用了它。

    以下是垂直线性布局的代码:

    private void measure() {
        if (this.getOrientation() == LinearLayout.VERTICAL) {
            int h = 0;
            int w = 0;
            this.measureChildren(0, 0);
            for (int i = 0; i < this.getChildCount(); i++) {
                View v = this.getChildAt(i);
                h += v.getMeasuredHeight();
                w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
            }
            height = (h < height) ? height : h;
            width = (w < width) ? width : w;
        }
        this.setMeasuredDimension(width, height);
    }
    
        3
  •  1
  •   Николай Сколунов    11 年前

    必须在style.xml中设置透明颜色

    <color name="transparent">#00000000</color>
    

    在代码中只需调用

    LinearLayout ll = (LinearLayout) findViewById(R.id.noteList);
    ll.setBackgroundColor(getResources().getColor(R.color.transparent));
    ll.invalidate();
    

    如果你有可提取的背景电话

    ll.setBackgroundResource(R.drawable.your_drawable);
    
    推荐文章