Android 添加视图后刷新 LinearLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2469466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refreshing a LinearLayout after adding a view
提问by lbedogni
I'm trying to add views dynamically to a linearlayout. I see through getChildCount() that the views are added to the layout, but even calling invalidate() on the layout doesn't give me the childs showed up.
我正在尝试将视图动态添加到线性布局。我通过 getChildCount() 看到视图已添加到布局中,但即使在布局上调用 invalidate() 也不会让我出现孩子。
Am I missing something?
我错过了什么吗?
回答by Klarth
A couple of things you can check in your code:
您可以在代码中检查几件事:
- On the Viewthat is being added, check that you call its setLayoutParametermethod with an appropriate ViewGroup.LayoutParameter.
- When you adding the new Views, make sure you are doing it on the UI thread. To do this, you can use the parent View's postmethod.
- 在正在添加的视图上,检查您是否使用适当的ViewGroup.LayoutParameter调用其setLayoutParameter方法。
- 添加新的View 时,请确保在 UI 线程上执行此操作。为此,您可以使用父View的post方法。
This self contained example adds a TextViewafter a short delay when it starts:
这个自包含的示例在启动时会在短暂延迟后添加一个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);
}
}
回答by Daniel
I had the same problem and noticed that my overriden onMeasure() method wasn't called after the invalidate. So I created my own subroutine in the LinearLayout and called it before the invalidate() method.
我遇到了同样的问题,并注意到在无效之后没有调用我覆盖的 onMeasure() 方法。所以我在 LinearLayout 中创建了我自己的子程序并在 invalidate() 方法之前调用它。
Here is the code for an vertical LinearLayout:
这是垂直 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);
}
回答by Николай Сколунов
I spent a lot of time to solve this problem too. And I found a simple method of refresh LinearLayout in 3 lines of code
我也花了很多时间来解决这个问题。而且我在3行代码中找到了一个简单的刷新LinearLayout的方法
You must set transperent color in style.xml
您必须在 style.xml 中设置透明颜色
<color name="transparent">#00000000</color>
And in the code just call to set background
在代码中只需调用设置背景
LinearLayout ll = (LinearLayout) findViewById(R.id.noteList);
ll.setBackgroundColor(getResources().getColor(R.color.transparent));
ll.invalidate();
If you has drawable background call
如果您有可绘制的后台调用
ll.setBackgroundResource(R.drawable.your_drawable);