在添加视图后刷新LinearLayout

15

我正在尝试动态地将视图添加到线性布局中。 通过getChildCount()查看,我发现视图已添加到布局中,但即使在布局上调用invalidate(),子项也没有显示出来。

我是否漏掉了什么?


1
展示一些代码,因为你可能做错了。 - Pentium10
我只需创建一个视图,将其添加到线性布局中并调用invalidate()。 - lbedogni
3个回答

22

您可以在代码中检查以下几点:

以下自包含示例会在启动后短暂延迟后添加一个 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);
    }
}

我在Handler中创建新的形状。 奇怪的事实是第一个形状被绘制在LinearLayout上,但接下来的形状却没有。 - lbedogni
请检查您是否已将android:orientation="vertical"添加到LinearLayout中。 - karabara

2

我遇到了同样的问题,发现在调用invalidate()方法后我的重写onMeasure()方法没有被调用。因此,我在LinearLayout中创建了自己的子程序,在调用invalidate()方法之前先调用它。

这是一个垂直LinearLayout的代码:

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);
}

你从哪里获取高度和宽度的? - Jemshit Iskenderov

1
我也花了很多时间解决这个问题。我发现只需三行代码就能简单地刷新LinearLayout。
你必须在style.xml中设置透明颜色。
<color name="transparent">#00000000</color>

在代码中调用设置背景的方法即可。

同时保留HTML标签。

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

如果您有可绘制的背景,请调用:
ll.setBackgroundResource(R.drawable.your_drawable);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接