Android 将ImageView动态添加到LinnearLayout时出错
我写了一个小函数Android 将ImageView动态添加到LinnearLayout时出错,android,android-layout,Android,Android Layout,我写了一个小函数 private void addTabIndicators(int tabCount){ LinearLayout indicatorsContainer = (LinearLayout)findViewById(R.id.indicators_container); for(int i = 0; i<tabCount; i++){ ImageView indicator = (ImageView)this.getLayoutInflate
private void addTabIndicators(int tabCount){
LinearLayout indicatorsContainer = (LinearLayout)findViewById(R.id.indicators_container);
for(int i = 0; i<tabCount; i++){
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
indicatorsContainer.addView(indicator);
}
}
private void addTabIndicators(int tabCount){
LinearLayout指示器容器=(LinearLayout)findViewById(R.id.indicators\U容器);
对于(int i=0;i而言,故障可能在这里
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
您需要传递图像视图的根视图以提供XML中定义的布局。如果改为传递null,则会设置默认的layoutparams
放置
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, your root view of image view,false);
这是一个非常常见的错误。除非你真的知道自己在做什么,否则不要传递null
请在此处阅读更多信息:
希望它有帮助太好了,工作起来很有魅力。另外,我发现如果我想使用ImageView indicator=(ImageView)这个。GetLayoutFlater()。充气(R.layout.tab_indicator,null);我必须将ImageView包装成一个相对物。很高兴我能帮助你@Chris
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, your root view of image view,false);