[英]Dynamically add multiple horizontal linearlayour to tab
I have a case where I need to add 2 TextViews to a horizontal LinearLayout, and replicate that structure numberous times. 我有一种情况,我需要将2个TextViews添加到水平LinearLayout中,并多次复制该结构。
For example: 例如:
|TextView1| | TextView1 | |TextView2| | TextView2 |
|TextView1| | TextView1 | |TextView2| | TextView2 |
etc.. 等等..
The code I have so far is: 到目前为止,我的代码是:
public View createTabContent(String tag){
LinearLayout mainTabLayout = new LinearLayout(Result.this);
mainTabLayout.setOrientation(LinearLayout.HORIZONTAL);
mainTabLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for( Object param : mainHashMap.values() ){
String key = param.toString();
LinearLayout linLayout = new LinearLayout(Result.this);
linLayout.setOrientation(LinearLayout.HORIZONTAL);
linLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tvKey = new TextView(Result.this);
tvKey.setText(key);
tvKey.setTextSize(15);
linLayout.addView(tvKey);
try{
String member = this.transformMember(key);
Method method = mainClass.getMethod("get" + member);
TextView tvValue = new TextView(Result.this);
tvValue.setText((method.invoke(mainData) != null) ? method.invoke(dvlaData).toString() : "");
tvValue.setTextSize(10);
linLayout.addView(tvValue);
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Log.d("ResultActivity", "adding to view");
mainTabLayout.addView(linLayout);
}
return mainTabLayout;
}
Log.d shows that it is running over every item it needs to, however the TabContent only have the last LinearLayout displaying. Log.d显示它正在需要的每个项目上运行,但是TabContent仅显示最后一个LinearLayout。 It's overwriting the previous LinearLayouts added to the mainTabLayout. 它覆盖了以前添加到mainTabLayout中的LinearLayouts。
I hope this makes sense... 我希望这是有道理的...
尝试将mainTabLayout方向设置为垂直而不是水平
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.