【问题标题】:Refreshing a LinearLayout after adding a view添加视图后刷新 LinearLayout
【发布时间】:2010-03-18 11:39:11
【问题描述】:

我正在尝试将视图动态添加到线性布局。 我通过 getChildCount() 看到视图已添加到布局中,但即使在布局上调用 invalidate() 也不会让孩子出现。

我错过了什么吗?

【问题讨论】:

  • 给我们看一些代码,因为你可能做错了。
  • 我只需创建一个视图,将其添加到线性布局并调用 invalidate()。

标签: android drawable android-linearlayout


【解决方案1】:

您可以在代码中检查几件事:

这个自包含的示例在启动时会在短暂延迟后添加一个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 中制作了新的形状。奇怪的事实是第一个形状是在线性布局上绘制的,但接下来的不是。
  • 检查您是否已将 android:orientation="vertical" 添加到 LinearLayout
【解决方案2】:

我遇到了同样的问题,并注意到我重写的 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】:

我也花了很多时间来解决这个问题。而且我发现了一个简单的方法,用3行代码刷新LinearLayout

您必须在 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多